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);
        }
Beispiel #2
0
        public static EventData ToEventData(this TraceEvent traceEvent, IHealthReporter healthReporter)
        {
            Debug.Assert(healthReporter != null);

            EventData eventData = new EventData
            {
                ProviderName = traceEvent.ProviderName,
                Timestamp    = traceEvent.TimeStamp.ToUniversalTime(),
                Level        = (LogLevel)(int)traceEvent.Level,
                Keywords     = (long)traceEvent.Keywords
            };

            IDictionary <string, object> payloadData = eventData.Payload;

            payloadData.Add(nameof(traceEvent.ID), (int)traceEvent.ID);   // TraceEvent.ID is ushort, not CLS-compliant, so we cast to int
            payloadData.Add(nameof(traceEvent.EventName), traceEvent.EventName);
            if (traceEvent.ActivityID != default(Guid))
            {
                payloadData.Add(nameof(traceEvent.ActivityID), ActivityPathDecoder.GetActivityPathString(traceEvent.ActivityID));
            }
            if (traceEvent.RelatedActivityID != default(Guid))
            {
                payloadData.Add(nameof(traceEvent.RelatedActivityID), traceEvent.RelatedActivityID.ToString());
            }
            // ProcessID and ProcessName are somewhat common property names, so to minimize likelihood of conflicts we use a prefix
            payloadData.Add("TraceEventProcessID", traceEvent.ProcessID);
            payloadData.Add("TraceEventProcessName", traceEvent.ProcessName);

            try
            {
                // If the event has a badly formatted manifest, the FormattedMessage property getter might throw
                string message = traceEvent.FormattedMessage;
                if (message != null)
                {
                    payloadData.Add("Message", traceEvent.FormattedMessage);
                }
            }
            catch { }

            traceEvent.ExtractPayloadData(eventData, healthReporter);

            return(eventData);
        }
        public static EventData ToEventData(this TraceEvent traceEvent, IHealthReporter healthReporter)
        {
            Debug.Assert(healthReporter != null);

            EventData eventData = new EventData
            {
                ProviderName = traceEvent.ProviderName,
                Timestamp    = DateTime.UtcNow,
                Level        = (LogLevel)(int)traceEvent.Level,
                Keywords     = (long)traceEvent.Keywords
            };

            IDictionary <string, object> payloadData = eventData.Payload;

            payloadData.Add(nameof(traceEvent.ID), (int)traceEvent.ID);   // TraceEvent.ID is ushort, not CLS-compliant, so we cast to int
            payloadData.Add(nameof(traceEvent.EventName), traceEvent.EventName);
            if (traceEvent.ActivityID != default(Guid))
            {
                payloadData.Add(nameof(traceEvent.ActivityID), ActivityPathDecoder.GetActivityPathString(traceEvent.ActivityID));
            }
            if (traceEvent.RelatedActivityID != default(Guid))
            {
                payloadData.Add(nameof(traceEvent.RelatedActivityID), traceEvent.RelatedActivityID.ToString());
            }

            try
            {
                // If the event has a badly formatted manifest, the FormattedMessage property getter might throw
                string message = traceEvent.FormattedMessage;
                if (message != null)
                {
                    payloadData.Add("Message", traceEvent.FormattedMessage);
                }
            }
            catch { }

            traceEvent.ExtractPayloadData(eventData, healthReporter);

            return(eventData);
        }