/// <summary> /// Контекст трассировки /// </summary> /// <param name="id">идентификатор трассировки</param> /// <param name="root">корневое событие трассировки</param> internal TraceContext(TraceId id , TraceEvent root) { if (id == null) { throw new ArgumentNullException("id"); } _id = id; _root = root; if (_root == null) { _root = TraceEvent.Create(_id, _id.Name); } }
/// <summary> /// Добавить событие /// </summary> /// <param name="name">сообщение</param> /// <param name="type">тип</param> public static void AddEvent(string name) { var current = Current; var @event = TraceEvent.Create(current.Id, name); current.Root.AddEvent(@event); var h = current.OnEvent; if (h != null) { h(current, new OnAppendEventArgs(@event)); } }
/// <summary> /// Окружение контекста трассировки /// </summary> /// <param name="eventName">имя события</param> /// <param name="mode"></param> public TraceContextScope(string eventName , TraceContextMode mode = TraceContextMode.Add) { if (Current != null) { Id = Current.Id; } else { Id = TraceId.Create(eventName); } Root = TraceEvent.Create(id: Id, name: eventName); Mode = mode; PushScope(); }
/// <summary> /// Перед отправкой сообщения включаем в заголовок информацию о текущей трассировке /// </summary> /// <param name="request">запрос</param> /// <param name="channel">канал</param> /// <returns></returns> public object BeforeSendRequest(ref Message request, IClientChannel channel) { var scope = TraceContextScope.Current; if (scope == null) { return(null); } var action = request.Headers.Action; action = action.Replace("http://tempuri.org/", ""); var id = scope.Id; var @event = TraceEvent.Create(id: id , name: "call> " + action); var callScope = new TraceContextScope(id, @event, TraceContextMode.Add); var index = request.Headers.FindHeader(TraceMeHeader.HeaderName, Namespace.Main); if (index > -1) { request.Headers.RemoveAt(index); } var header = MessageHeader.CreateHeader( TraceMeHeader.HeaderName, Namespace.Main , new TraceMeHeader() { Id = id, }); request.Headers.Add(header); return(callScope); }
/// <summary> /// После принятия запроса если присутствует флаг TraceMe, создаем окружение трассировки /// </summary> /// <param name="request"></param> /// <param name="channel"></param> /// <param name="instanceContext"></param> /// <returns></returns> public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { var index = request.Headers.FindHeader(TraceMeHeader.HeaderName, Namespace.Main); if (index < 0) { return(null); } var header = request.Headers.GetHeader <TraceMeHeader>(index); if (header == null) { return(null); } var @event = TraceEvent.Create( id: header.Id , name: "disp> " + instanceContext.Host.Description.Name); @event["host"] = HostName; return(new TraceContextScope(header.Id, @event, TraceContextMode.Add)); }