public static void Track(this TraceEvent traceEvent, TelemetryClient client) { Debug.Assert(client != null, "Should always receive a valid client"); string formattedMessage = traceEvent.FormattedMessage; TraceTelemetry telemetry = new TraceTelemetry(formattedMessage, traceEventLevelToSeverityLevel[(int)traceEvent.Level]); telemetry.AddProperty("EventId", traceEvent.ID.ToString()); telemetry.AddProperty(nameof(traceEvent.EventName), traceEvent.EventName); if (traceEvent.ActivityID != default(Guid)) { telemetry.AddProperty(nameof(traceEvent.ActivityID), ActivityPathDecoder.GetActivityPathString(traceEvent.ActivityID)); } if (traceEvent.RelatedActivityID != default(Guid)) { telemetry.AddProperty(nameof(traceEvent.RelatedActivityID), traceEvent.RelatedActivityID.ToString()); } telemetry.AddProperty(nameof(traceEvent.Channel), traceEvent.Channel.ToString()); telemetry.AddProperty(nameof(traceEvent.Keywords), GetHexRepresentation((long)traceEvent.Keywords)); telemetry.AddProperty(nameof(traceEvent.Opcode), traceEvent.Opcode.ToString()); if (traceEvent.Task != TraceEventTask.Default) { telemetry.AddProperty(nameof(traceEvent.Task), GetHexRepresentation((int)traceEvent.Task)); } // Make this the call after adding traceEvent properties that property name on trace event will take the priority in possible duplicated property scenario. traceEvent.ExtractPayloadData(telemetry); client.Track(telemetry); }
/// <summary> /// Creates a TraceTelemetry out of an EventSource event and tracks it using the supplied client. /// </summary> /// <param name="eventSourceEvent">The source for the telemetry data.</param> /// <param name="client">Client to track the data with.</param> public static void Track(this EventWrittenEventArgs eventSourceEvent, TelemetryClient client) { Debug.Assert(client != null, "Should always receive a valid client"); string formattedMessage = null; if (eventSourceEvent.Message != null) { try { // If the event has a badly formatted manifest, message formatting might fail formattedMessage = string.Format(CultureInfo.InvariantCulture, eventSourceEvent.Message, eventSourceEvent.Payload.ToArray()); } catch { } } TraceTelemetry telemetry = new TraceTelemetry(formattedMessage, eventLevelToSeverityLevel[(int)eventSourceEvent.Level]); eventSourceEvent.ExtractPayloadData(telemetry); telemetry.AddProperty(nameof(EventWrittenEventArgs.EventId), eventSourceEvent.EventId.ToString(CultureInfo.InvariantCulture)); telemetry.AddProperty(nameof(EventWrittenEventArgs.EventName), eventSourceEvent.EventName); if (eventSourceEvent.ActivityId != default(Guid)) { telemetry.AddProperty(nameof(EventWrittenEventArgs.ActivityId), ActivityPathDecoder.GetActivityPathString(eventSourceEvent.ActivityId)); } if (eventSourceEvent.RelatedActivityId != default(Guid)) { telemetry.AddProperty(nameof(EventWrittenEventArgs.RelatedActivityId), eventSourceEvent.RelatedActivityId.ToString()); } telemetry.AddProperty(nameof(EventWrittenEventArgs.Channel), eventSourceEvent.Channel.GetChannelName()); telemetry.AddProperty(nameof(EventWrittenEventArgs.Keywords), GetHexRepresentation((long)eventSourceEvent.Keywords)); telemetry.AddProperty(nameof(EventWrittenEventArgs.Opcode), eventSourceEvent.Opcode.GetOpcodeName()); if (eventSourceEvent.Tags != EventTags.None) { telemetry.AddProperty(nameof(EventWrittenEventArgs.Tags), GetHexRepresentation((int)eventSourceEvent.Tags)); } if (eventSourceEvent.Task != EventTask.None) { telemetry.AddProperty(nameof(EventWrittenEventArgs.Task), GetHexRepresentation((int)eventSourceEvent.Task)); } client.Track(telemetry); }
private static void ExtractPayloadData(this TraceEvent traceEvent, TraceTelemetry telemetry) { Debug.Assert(telemetry != null, "Should have received a valid TraceTelemetry object"); if (traceEvent.PayloadNames == null) { return; } foreach (string payloadName in traceEvent.PayloadNames) { telemetry.AddProperty(payloadName, traceEvent.PayloadByName(payloadName).ToString()); } }
/// <summary> /// Populates a standard set of properties on the <see cref="TraceTelemetry"/> with values from the a given EventSource event. /// </summary> /// <param name="telemetry">Telemetry item to populate with properties.</param> /// <param name="eventSourceEvent">Event to extract values from.</param> public static TraceTelemetry PopulateStandardProperties(this TraceTelemetry telemetry, EventWrittenEventArgs eventSourceEvent) { if (!string.IsNullOrWhiteSpace(eventSourceEvent.EventSource.Name)) { telemetry.AddProperty(ProviderNameProperty, eventSourceEvent.EventSource.Name); } if (eventSourceEvent.EventSource.Guid != Guid.Empty) { telemetry.AddProperty(ProviderGuidProperty, eventSourceEvent.EventSource.Guid.ToString()); } telemetry.AddProperty(nameof(EventWrittenEventArgs.EventId), eventSourceEvent.EventId.ToString(CultureInfo.InvariantCulture)); telemetry.AddProperty(nameof(EventWrittenEventArgs.EventName), eventSourceEvent.EventName); if (eventSourceEvent.ActivityId != default(Guid)) { telemetry.AddProperty(nameof(EventWrittenEventArgs.ActivityId), ActivityPathDecoder.GetActivityPathString(eventSourceEvent.ActivityId)); } if (eventSourceEvent.RelatedActivityId != default(Guid)) { telemetry.AddProperty(nameof(EventWrittenEventArgs.RelatedActivityId), ActivityPathDecoder.GetActivityPathString(eventSourceEvent.RelatedActivityId)); } telemetry.AddProperty(nameof(EventWrittenEventArgs.Channel), eventSourceEvent.Channel.GetChannelName()); telemetry.AddProperty(nameof(EventWrittenEventArgs.Keywords), GetHexRepresentation((long)eventSourceEvent.Keywords)); telemetry.AddProperty(nameof(EventWrittenEventArgs.Opcode), eventSourceEvent.Opcode.GetOpcodeName()); if (eventSourceEvent.Tags != EventTags.None) { telemetry.AddProperty(nameof(EventWrittenEventArgs.Tags), GetHexRepresentation((int)eventSourceEvent.Tags)); } if (eventSourceEvent.Task != EventTask.None) { telemetry.AddProperty(nameof(EventWrittenEventArgs.Task), GetHexRepresentation((int)eventSourceEvent.Task)); } return(telemetry); }