Beispiel #1
0
        public static EventData ToEventData(this EventWrittenEventArgs eventSourceEvent, IHealthReporter healthReporter, string context)
        {
            Debug.Assert(healthReporter != null);

            // High-precision event timestamping is availabe on .NET 4.6+ and .NET Core 2.0+
            // For the latter the implementation of DateTime.UtcNow has changed and we do not need to do anything.
            // .NET Core 1.1 will use imprecise timestamp--there is no easy fix for this target.

#if NETSTANDARD1_6
            DateTime now = DateTime.UtcNow;
#else
            DateTime now;
            if (hasPreciseTime)
            {
                GetSystemTimePreciseAsFileTime(out long filetime);
                now = DateTime.FromFileTimeUtc(filetime);
            }
            else
            {
                now = DateTime.UtcNow;
            }
#endif
            EventData eventData = new EventData
            {
                ProviderName = eventSourceEvent.EventSource.Name,
                Timestamp    = now,
                Level        = (LogLevel)(int)eventSourceEvent.Level,
                Keywords     = (long)eventSourceEvent.Keywords
            };

            IDictionary <string, object> payloadData = eventData.Payload;
            payloadData.Add(nameof(eventSourceEvent.EventId), eventSourceEvent.EventId);
            payloadData.Add(nameof(eventSourceEvent.EventName), eventSourceEvent.EventName);
            if (eventSourceEvent.ActivityId != default(Guid))
            {
                payloadData.Add(nameof(EventWrittenEventArgs.ActivityId), ActivityPathDecoder.GetActivityPathString(eventSourceEvent.ActivityId));
            }
            if (eventSourceEvent.RelatedActivityId != default(Guid))
            {
                payloadData.Add(nameof(EventWrittenEventArgs.RelatedActivityId), ActivityPathDecoder.GetActivityPathString(eventSourceEvent.RelatedActivityId));
            }

            try
            {
                if (eventSourceEvent.Message != null)
                {
                    // If the event has a badly formatted manifest, the FormattedMessage property getter might throw
                    payloadData.Add(nameof(eventSourceEvent.Message), string.Format(CultureInfo.InvariantCulture, eventSourceEvent.Message, eventSourceEvent.Payload.ToArray()));
                }
            }
            catch { }

            eventSourceEvent.ExtractPayloadData(eventData, healthReporter, context);

            return(eventData);
        }
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);
        }
        public static EventData ToEventData(this EventWrittenEventArgs eventSourceEvent, IHealthReporter healthReporter, string context)
        {
            Debug.Assert(healthReporter != null);

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

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

            payloadData.Add(nameof(eventSourceEvent.EventId), eventSourceEvent.EventId);
            payloadData.Add(nameof(eventSourceEvent.EventName), eventSourceEvent.EventName);
            if (eventSourceEvent.ActivityId != default(Guid))
            {
                payloadData.Add(nameof(EventWrittenEventArgs.ActivityId), ActivityPathDecoder.GetActivityPathString(eventSourceEvent.ActivityId));
            }
            if (eventSourceEvent.RelatedActivityId != default(Guid))
            {
                payloadData.Add(nameof(EventWrittenEventArgs.RelatedActivityId), eventSourceEvent.RelatedActivityId.ToString());
            }

            try
            {
                if (eventSourceEvent.Message != null)
                {
                    // If the event has a badly formatted manifest, the FormattedMessage property getter might throw
                    payloadData.Add(nameof(eventSourceEvent.Message), string.Format(CultureInfo.InvariantCulture, eventSourceEvent.Message, eventSourceEvent.Payload.ToArray()));
                }
            }
            catch { }

            eventSourceEvent.ExtractPayloadData(eventData, healthReporter, context);

            return(eventData);
        }