@@ -11,6 +11,13 @@ namespace System.Diagnostics.Tracing
11
11
{
12
12
public partial class EventSource
13
13
{
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
+
14
21
// ActivityID support (see also WriteEventWithRelatedActivityIdCore)
15
22
/// <summary>
16
23
/// When a thread starts work that is on behalf of 'something else' (typically another
@@ -33,16 +40,20 @@ public static void SetCurrentThreadActivityId(Guid activityId)
33
40
{
34
41
if ( TplEtwProvider . Log != null )
35
42
TplEtwProvider . Log . SetActivityId ( activityId ) ;
36
- #if FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
43
+ #if FEATURE_MANAGED_ETW
37
44
#if FEATURE_ACTIVITYSAMPLING
38
45
Guid newId = activityId ;
39
46
#endif // FEATURE_ACTIVITYSAMPLING
40
47
// We ignore errors to keep with the convention that EventSources do not throw errors.
41
48
// Note we can't access m_throwOnWrites because this is a static method.
42
49
50
+ #if FEATURE_PERFTRACING
51
+ s_currentThreadActivityId = activityId ;
52
+ #elif PLATFORM_WINDOWS
43
53
if ( UnsafeNativeMethods . ManifestEtw . EventActivityIdControl (
44
54
UnsafeNativeMethods . ManifestEtw . ActivityControl . EVENT_ACTIVITY_CTRL_GET_SET_ID ,
45
55
ref activityId ) == 0 )
56
+ #endif // FEATURE_PERFTRACING
46
57
{
47
58
#if FEATURE_ACTIVITYSAMPLING
48
59
var activityDying = s_activityDying ;
@@ -57,7 +68,7 @@ public static void SetCurrentThreadActivityId(Guid activityId)
57
68
}
58
69
#endif // FEATURE_ACTIVITYSAMPLING
59
70
}
60
- #endif // FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
71
+ #endif // FEATURE_MANAGED_ETW
61
72
}
62
73
63
74
/// <summary>
@@ -82,14 +93,19 @@ public static void SetCurrentThreadActivityId(Guid activityId)
82
93
public static void SetCurrentThreadActivityId ( Guid activityId , out Guid oldActivityThatWillContinue )
83
94
{
84
95
oldActivityThatWillContinue = activityId ;
85
- #if FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
96
+ #if FEATURE_MANAGED_ETW
86
97
// We ignore errors to keep with the convention that EventSources do not throw errors.
87
98
// Note we can't access m_throwOnWrites because this is a static method.
88
99
100
+ #if FEATURE_PERFTRACING
101
+ oldActivityThatWillContinue = s_currentThreadActivityId ;
102
+ s_currentThreadActivityId = activityId ;
103
+ #elif PLATFORM_WINDOWS
89
104
UnsafeNativeMethods . ManifestEtw . EventActivityIdControl (
90
105
UnsafeNativeMethods . ManifestEtw . ActivityControl . EVENT_ACTIVITY_CTRL_GET_SET_ID ,
91
106
ref oldActivityThatWillContinue ) ;
92
- #endif // FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
107
+ #endif // FEATURE_PERFTRACING
108
+ #endif // FEATURE_MANAGED_ETW
93
109
94
110
// We don't call the activityDying callback here because the caller has declared that
95
111
// it is not dying.
@@ -107,11 +123,15 @@ public static Guid CurrentThreadActivityId
107
123
// We ignore errors to keep with the convention that EventSources do not throw
108
124
// errors. Note we can't access m_throwOnWrites because this is a static method.
109
125
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
111
130
UnsafeNativeMethods . ManifestEtw . EventActivityIdControl (
112
131
UnsafeNativeMethods . ManifestEtw . ActivityControl . EVENT_ACTIVITY_CTRL_GET_ID ,
113
132
ref retVal ) ;
114
- #endif // FEATURE_MANAGED_ETW && PLATFORM_WINDOWS
133
+ #endif // FEATURE_PERFTRACING
134
+ #endif // FEATURE_MANAGED_ETW
115
135
return retVal ;
116
136
}
117
137
}
0 commit comments