@@ -27,15 +27,14 @@ internal class HttpInListener : ListenerHandler
27
27
internal const string OnUnhandledHostingExceptionEvent = "Microsoft.AspNetCore.Hosting.UnhandledException" ;
28
28
internal const string OnUnHandledDiagnosticsExceptionEvent = "Microsoft.AspNetCore.Diagnostics.UnhandledException" ;
29
29
30
- #if NET7_0_OR_GREATER
31
30
// https://github.com/dotnet/aspnetcore/blob/8d6554e655b64da75b71e0e20d6db54a3ba8d2fb/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs#L85
32
31
internal static readonly string AspNetCoreActivitySourceName = "Microsoft.AspNetCore" ;
33
- #endif
34
32
35
33
internal static readonly AssemblyName AssemblyName = typeof ( HttpInListener ) . Assembly . GetName ( ) ;
36
34
internal static readonly string ActivitySourceName = AssemblyName . Name ;
37
35
internal static readonly Version Version = AssemblyName . Version ;
38
36
internal static readonly ActivitySource ActivitySource = new ( ActivitySourceName , Version . ToString ( ) ) ;
37
+ internal static readonly bool Net7OrGreater = Environment . Version . Major >= 7 ;
39
38
40
39
private const string DiagnosticSourceName = "Microsoft.AspNetCore" ;
41
40
@@ -121,14 +120,19 @@ public void OnStartActivity(Activity activity, object payload)
121
120
// Create a new activity with its parent set from the extracted context.
122
121
// This makes the new activity as a "sibling" of the activity created by
123
122
// Asp.Net Core.
124
- #if NET7_0_OR_GREATER
125
- // For NET7.0 onwards activity is created using ActivitySource so,
126
- // we will use the source of the activity to create the new one.
127
- Activity newOne = activity . Source . CreateActivity ( ActivityOperationName , ActivityKind . Server , ctx . ActivityContext ) ;
128
- #else
129
- Activity newOne = new Activity ( ActivityOperationName ) ;
130
- newOne . SetParentId ( ctx . ActivityContext . TraceId , ctx . ActivityContext . SpanId , ctx . ActivityContext . TraceFlags ) ;
131
- #endif
123
+ Activity newOne ;
124
+ if ( Net7OrGreater )
125
+ {
126
+ // For NET7.0 onwards activity is created using ActivitySource so,
127
+ // we will use the source of the activity to create the new one.
128
+ newOne = activity . Source . CreateActivity ( ActivityOperationName , ActivityKind . Server , ctx . ActivityContext ) ;
129
+ }
130
+ else
131
+ {
132
+ newOne = new Activity ( ActivityOperationName ) ;
133
+ newOne . SetParentId ( ctx . ActivityContext . TraceId , ctx . ActivityContext . SpanId , ctx . ActivityContext . TraceFlags ) ;
134
+ }
135
+
132
136
newOne . TraceStateString = ctx . ActivityContext . TraceState ;
133
137
134
138
newOne . SetTag ( "IsCreatedByInstrumentation" , bool . TrueString ) ;
@@ -166,10 +170,11 @@ public void OnStartActivity(Activity activity, object payload)
166
170
return ;
167
171
}
168
172
169
- #if ! NET7_0_OR_GREATER
170
- ActivityInstrumentationHelper . SetActivitySourceProperty ( activity , ActivitySource ) ;
171
- ActivityInstrumentationHelper . SetKindProperty ( activity , ActivityKind . Server ) ;
172
- #endif
173
+ if ( ! Net7OrGreater )
174
+ {
175
+ ActivityInstrumentationHelper . SetActivitySourceProperty ( activity , ActivitySource ) ;
176
+ ActivityInstrumentationHelper . SetKindProperty ( activity , ActivityKind . Server ) ;
177
+ }
173
178
174
179
var path = ( request . PathBase . HasValue || request . Path . HasValue ) ? ( request . PathBase + request . Path ) . ToString ( ) : "/" ;
175
180
activity . DisplayName = GetDisplayName ( request . Method ) ;
@@ -263,12 +268,17 @@ public void OnStopActivity(Activity activity, object payload)
263
268
}
264
269
}
265
270
266
- #if NET7_0_OR_GREATER
267
- var tagValue = activity . GetTagValue ( "IsCreatedByInstrumentation" ) ;
271
+ object tagValue ;
272
+ if ( Net7OrGreater )
273
+ {
274
+ tagValue = activity . GetTagValue ( "IsCreatedByInstrumentation" ) ;
275
+ }
276
+ else
277
+ {
278
+ _ = activity . TryCheckFirstTag ( "IsCreatedByInstrumentation" , out tagValue ) ;
279
+ }
280
+
268
281
if ( ReferenceEquals ( tagValue , bool . TrueString ) )
269
- #else
270
- if ( activity . TryCheckFirstTag ( "IsCreatedByInstrumentation" , out var tagValue ) && ReferenceEquals ( tagValue , bool . TrueString ) )
271
- #endif
272
282
{
273
283
// If instrumentation started a new Activity, it must
274
284
// be stopped here.
0 commit comments