@@ -54,14 +54,27 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
54
54
{
55
55
long startTimestamp = 0 ;
56
56
57
- if ( _eventSource . IsEnabled ( ) || _metrics . IsEnabled ( ) )
57
+ if ( _metrics . IsEnabled ( ) )
58
58
{
59
- context . EventLogOrMetricsEnabled = true ;
59
+ context . MetricsEnabled = true ;
60
60
context . MetricsTagsFeature ??= new HttpMetricsTagsFeature ( ) ;
61
61
httpContext . Features . Set < IHttpMetricsTagsFeature > ( context . MetricsTagsFeature ) ;
62
62
63
63
startTimestamp = Stopwatch . GetTimestamp ( ) ;
64
64
65
+ // To keep the hot path short we defer logging in this function to non-inlines
66
+ RecordRequestStartMetrics ( httpContext ) ;
67
+ }
68
+
69
+ if ( _eventSource . IsEnabled ( ) )
70
+ {
71
+ context . EventLogEnabled = true ;
72
+
73
+ if ( startTimestamp == 0 )
74
+ {
75
+ startTimestamp = Stopwatch . GetTimestamp ( ) ;
76
+ }
77
+
65
78
// To keep the hot path short we defer logging in this function to non-inlines
66
79
RecordRequestStartEventLog ( httpContext ) ;
67
80
}
@@ -135,7 +148,7 @@ public void RequestEnd(HttpContext httpContext, Exception? exception, HostingApp
135
148
// Non-inline
136
149
LogRequestFinished ( context , startTimestamp , currentTimestamp ) ;
137
150
138
- if ( context . EventLogOrMetricsEnabled )
151
+ if ( context . MetricsEnabled )
139
152
{
140
153
var route = httpContext . GetEndpoint ( ) ? . Metadata . GetMetadata < IRouteDiagnosticsMetadata > ( ) ? . Route ;
141
154
var customTags = context . MetricsTagsFeature ? . TagsList ;
@@ -197,7 +210,7 @@ public void RequestEnd(HttpContext httpContext, Exception? exception, HostingApp
197
210
StopActivity ( httpContext , activity , context . HasDiagnosticListener ) ;
198
211
}
199
212
200
- if ( context . EventLogOrMetricsEnabled )
213
+ if ( context . EventLogEnabled )
201
214
{
202
215
if ( exception != null )
203
216
{
@@ -219,7 +232,7 @@ public void RequestEnd(HttpContext httpContext, Exception? exception, HostingApp
219
232
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
220
233
public void ContextDisposed ( HostingApplication . Context context )
221
234
{
222
- if ( context . EventLogOrMetricsEnabled )
235
+ if ( context . EventLogEnabled )
223
236
{
224
237
_eventSource . RequestStop ( ) ;
225
238
}
@@ -352,10 +365,15 @@ internal UnhandledExceptionData(HttpContext httpContext, long timestamp, Excepti
352
365
[ MethodImpl ( MethodImplOptions . NoInlining ) ]
353
366
private void RecordRequestStartEventLog ( HttpContext httpContext )
354
367
{
355
- _metrics . RequestStart ( httpContext . Request . IsHttps , httpContext . Request . Scheme , httpContext . Request . Method , httpContext . Request . Host ) ;
356
368
_eventSource . RequestStart ( httpContext . Request . Method , httpContext . Request . Path ) ;
357
369
}
358
370
371
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
372
+ private void RecordRequestStartMetrics ( HttpContext httpContext )
373
+ {
374
+ _metrics . RequestStart ( httpContext . Request . IsHttps , httpContext . Request . Scheme , httpContext . Request . Method , httpContext . Request . Host ) ;
375
+ }
376
+
359
377
[ MethodImpl ( MethodImplOptions . NoInlining ) ]
360
378
private Activity ? StartActivity ( HttpContext httpContext , bool loggingEnabled , bool diagnosticListenerActivityCreationEnabled , out bool hasDiagnosticListener )
361
379
{
0 commit comments