From 704b9c86818b56637e4772a636e8935d61235bdf Mon Sep 17 00:00:00 2001 From: Brian Robbins Date: Tue, 13 Mar 2018 16:18:59 -0700 Subject: [PATCH] Plumb the activity ID through to EventListener. --- .../System/Diagnostics/Tracing/EventSource.cs | 38 +++++++++++++++------- .../TraceLogging/TraceLoggingEventSource.cs | 8 +++-- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/mscorlib/shared/System/Diagnostics/Tracing/EventSource.cs b/src/mscorlib/shared/System/Diagnostics/Tracing/EventSource.cs index 8547d77..70c65a0 100644 --- a/src/mscorlib/shared/System/Diagnostics/Tracing/EventSource.cs +++ b/src/mscorlib/shared/System/Diagnostics/Tracing/EventSource.cs @@ -111,13 +111,13 @@ // // On output there are the following routines // Writing to all listeners that are NOT ETW, we have the following routines -// * WriteToAllListeners(ID, Guid*, COUNT, EventData*) -// * WriteToAllListeners(ID, Guid*, object[]) -// * WriteToAllListeners(NAME, Guid*, EventPayload) +// * WriteToAllListeners(ID, Guid*, Guid*, COUNT, EventData*) +// * WriteToAllListeners(ID, Guid*, Guid*, object[]) +// * WriteToAllListeners(NAME, Guid*, Guid*, EventPayload) // // EventPayload is the internal type that implements the IDictionary interface // The EventListeners will pass back for serialized classes for nested object, but -// WriteToAllListeners(NAME, Guid*, EventPayload) unpacks this uses the fields as if they +// WriteToAllListeners(NAME, Guid*, Guid*, EventPayload) unpacks this uses the fields as if they // were parameters to a method. // // The first two are used for the WriteEvent* case, and the later is used for the Write case. @@ -1261,7 +1261,7 @@ namespace System.Diagnostics.Tracing #endif // FEATURE_MANAGED_ETW if (m_Dispatchers != null && m_eventData[eventId].EnabledForAnyListener) - WriteToAllListeners(eventId, relatedActivityId, eventDataCount, data); + WriteToAllListeners(eventId, pActivityId, relatedActivityId, eventDataCount, data); } catch (Exception ex) { @@ -1935,13 +1935,13 @@ namespace System.Diagnostics.Tracing // Maintain old behavior - object identity is preserved if (AppContextSwitches.PreserveEventListnerObjectIdentity) { - WriteToAllListeners(eventId, childActivityID, args); + WriteToAllListeners(eventId, pActivityId, childActivityID, args); } else #endif // !ES_BUILD_STANDALONE { object[] serializedArgs = SerializeEventArgs(eventId, args); - WriteToAllListeners(eventId, childActivityID, serializedArgs); + WriteToAllListeners(eventId, pActivityId, childActivityID, serializedArgs); } } } @@ -2012,7 +2012,7 @@ namespace System.Diagnostics.Tracing #endif //!ES_BUILD_PCL } - private unsafe void WriteToAllListeners(int eventId, Guid* childActivityID, int eventDataCount, EventSource.EventData* data) + private unsafe void WriteToAllListeners(int eventId, Guid* activityID, Guid* childActivityID, int eventDataCount, EventSource.EventData* data) { // We represent a byte[] as a integer denoting the length and then a blob of bytes in the data pointer. This causes a spurious // warning because eventDataCount is off by one for the byte[] case since a byte[] has 2 items associated it. So we want to check @@ -2043,14 +2043,16 @@ namespace System.Diagnostics.Tracing EventSource.EventData* dataPtr = data; for (int i = 0; i < paramCount; i++) args[i] = DecodeObject(eventId, i, ref dataPtr); - WriteToAllListeners(eventId, childActivityID, args); + WriteToAllListeners(eventId, activityID, childActivityID, args); } // helper for writing to all EventListeners attached the current eventSource. - private unsafe void WriteToAllListeners(int eventId, Guid* childActivityID, params object[] args) + private unsafe void WriteToAllListeners(int eventId, Guid* activityID, Guid* childActivityID, params object[] args) { EventWrittenEventArgs eventCallbackArgs = new EventWrittenEventArgs(this); eventCallbackArgs.EventId = eventId; + if (activityID != null) + eventCallbackArgs.ActivityId = *activityID; if (childActivityID != null) eventCallbackArgs.RelatedActivityId = *childActivityID; eventCallbackArgs.EventName = m_eventData[eventId].Name; @@ -4401,7 +4403,20 @@ namespace System.Diagnostics.Tracing /// public Guid ActivityId { - get { return EventSource.CurrentThreadActivityId; } + get + { + Guid activityId = m_activityId; + if (activityId == Guid.Empty) + { + activityId = EventSource.CurrentThreadActivityId; + } + + return activityId; + } + internal set + { + m_activityId = value; + } } /// @@ -4576,6 +4591,7 @@ namespace System.Diagnostics.Tracing private string m_eventName; private EventSource m_eventSource; private ReadOnlyCollection m_payloadNames; + private Guid m_activityId; internal EventTags m_tags; internal EventOpcode m_opcode; internal EventLevel m_level; diff --git a/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/TraceLoggingEventSource.cs b/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/TraceLoggingEventSource.cs index 10b378c..ed2690a 100644 --- a/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/TraceLoggingEventSource.cs +++ b/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/TraceLoggingEventSource.cs @@ -671,7 +671,7 @@ namespace System.Diagnostics.Tracing if (m_Dispatchers != null) { var eventData = (EventPayload)(eventTypes.typeInfos[0].GetData(data)); - WriteToAllListeners(eventName, ref descriptor, nameInfo.tags, pActivityId, eventData); + WriteToAllListeners(eventName, ref descriptor, nameInfo.tags, pActivityId, pRelatedActivityId, eventData); } } @@ -700,7 +700,7 @@ namespace System.Diagnostics.Tracing } } - private unsafe void WriteToAllListeners(string eventName, ref EventDescriptor eventDescriptor, EventTags tags, Guid* pActivityId, EventPayload payload) + private unsafe void WriteToAllListeners(string eventName, ref EventDescriptor eventDescriptor, EventTags tags, Guid* pActivityId, Guid* pChildActivityId, EventPayload payload) { EventWrittenEventArgs eventCallbackArgs = new EventWrittenEventArgs(this); eventCallbackArgs.EventName = eventName; @@ -712,7 +712,9 @@ namespace System.Diagnostics.Tracing // Self described events do not have an id attached. We mark it internally with -1. eventCallbackArgs.EventId = -1; if (pActivityId != null) - eventCallbackArgs.RelatedActivityId = *pActivityId; + eventCallbackArgs.ActivityId = *pActivityId; + if (pChildActivityId != null) + eventCallbackArgs.RelatedActivityId = *pChildActivityId; if (payload != null) { -- 2.7.4