@@ -11,6 +11,13 @@ namespace System.Diagnostics.Tracing
1111{
1212 public partial class EventSource
1313 {
14+ #if FEATURE_MANAGED_ETW && FEATURE_PERFTRACING
15+ // For non-Windows, we use a thread-local variable to hold the activity ID.
16+ // On Windows, ETW has it's own thread-local variable and we participate in its use.
17+ [ ThreadStatic ]
18+ private static Guid s_currentThreadActivityId ;
19+ #endif // FEATURE_MANAGED_ETW && FEATURE_PERFTRACING
20+
1421 // ActivityID support (see also WriteEventWithRelatedActivityIdCore)
1522 /// <summary>
1623 /// When a thread starts work that is on behalf of 'something else' (typically another
@@ -33,16 +40,20 @@ public static void SetCurrentThreadActivityId(Guid activityId)
3340 {
3441 if ( TplEtwProvider . Log != null )
3542 TplEtwProvider . Log . SetActivityId ( activityId ) ;
36- #if FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
43+ #if FEATURE_MANAGED_ETW
3744#if FEATURE_ACTIVITYSAMPLING
3845 Guid newId = activityId ;
3946#endif // FEATURE_ACTIVITYSAMPLING
4047 // We ignore errors to keep with the convention that EventSources do not throw errors.
4148 // Note we can't access m_throwOnWrites because this is a static method.
4249
50+ #if FEATURE_PERFTRACING
51+ s_currentThreadActivityId = activityId ;
52+ #elif PLATFORM_WINDOWS
4353 if ( UnsafeNativeMethods . ManifestEtw . EventActivityIdControl (
4454 UnsafeNativeMethods . ManifestEtw . ActivityControl . EVENT_ACTIVITY_CTRL_GET_SET_ID ,
4555 ref activityId ) == 0 )
56+ #endif // FEATURE_PERFTRACING
4657 {
4758#if FEATURE_ACTIVITYSAMPLING
4859 var activityDying = s_activityDying ;
@@ -57,7 +68,7 @@ public static void SetCurrentThreadActivityId(Guid activityId)
5768 }
5869#endif // FEATURE_ACTIVITYSAMPLING
5970 }
60- #endif // FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
71+ #endif // FEATURE_MANAGED_ETW
6172 }
6273
6374 /// <summary>
@@ -82,14 +93,19 @@ public static void SetCurrentThreadActivityId(Guid activityId)
8293 public static void SetCurrentThreadActivityId ( Guid activityId , out Guid oldActivityThatWillContinue )
8394 {
8495 oldActivityThatWillContinue = activityId ;
85- #if FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
96+ #if FEATURE_MANAGED_ETW
8697 // We ignore errors to keep with the convention that EventSources do not throw errors.
8798 // Note we can't access m_throwOnWrites because this is a static method.
8899
100+ #if FEATURE_PERFTRACING
101+ oldActivityThatWillContinue = s_currentThreadActivityId ;
102+ s_currentThreadActivityId = activityId ;
103+ #elif PLATFORM_WINDOWS
89104 UnsafeNativeMethods . ManifestEtw . EventActivityIdControl (
90105 UnsafeNativeMethods . ManifestEtw . ActivityControl . EVENT_ACTIVITY_CTRL_GET_SET_ID ,
91106 ref oldActivityThatWillContinue ) ;
92- #endif // FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
107+ #endif // FEATURE_PERFTRACING
108+ #endif // FEATURE_MANAGED_ETW
93109
94110 // We don't call the activityDying callback here because the caller has declared that
95111 // it is not dying.
@@ -107,11 +123,15 @@ public static Guid CurrentThreadActivityId
107123 // We ignore errors to keep with the convention that EventSources do not throw
108124 // errors. Note we can't access m_throwOnWrites because this is a static method.
109125 Guid retVal = new Guid ( ) ;
110- #if FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
126+ #if FEATURE_MANAGED_ETW
127+ #if FEATURE_PERFTRACING
128+ retVal = s_currentThreadActivityId ;
129+ #elif PLATFORM_WINDOWS
111130 UnsafeNativeMethods . ManifestEtw . EventActivityIdControl (
112131 UnsafeNativeMethods . ManifestEtw . ActivityControl . EVENT_ACTIVITY_CTRL_GET_ID ,
113132 ref retVal ) ;
114- #endif // FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
133+ #endif // FEATURE_PERFTRACING
134+ #endif // FEATURE_MANAGED_ETW
115135 return retVal ;
116136 }
117137 }
0 commit comments