11namespace Microsoft . ApplicationInsights . AspNetCore . DiagnosticListeners
22{
33 using System ;
4+ using System . Collections . Generic ;
45 using System . Diagnostics ;
56 using System . Globalization ;
67 using System . Linq ;
78 using System . Net . Http . Headers ;
89 using System . Text ;
910 using Extensibility . Implementation . Tracing ;
11+ using Microsoft . ApplicationInsights . AspNetCore . DiagnosticListeners . Implementation ;
1012 using Microsoft . ApplicationInsights . AspNetCore . Extensions ;
1113 using Microsoft . ApplicationInsights . Common ;
1214 using Microsoft . ApplicationInsights . DataContracts ;
1315 using Microsoft . ApplicationInsights . Extensibility ;
1416 using Microsoft . ApplicationInsights . Extensibility . Implementation ;
1517 using Microsoft . ApplicationInsights . Extensibility . W3C ;
1618 using Microsoft . AspNetCore . Http ;
17- using Microsoft . Extensions . DiagnosticAdapter ;
1819 using Microsoft . Extensions . Primitives ;
1920
2021 /// <summary>
@@ -38,6 +39,24 @@ internal class HostingDiagnosticListener : IApplicationInsightDiagnosticListener
3839 private static readonly ActiveSubsciptionManager SubscriptionManager = new ActiveSubsciptionManager ( ) ;
3940 private const string ActivityCreatedByHostingDiagnosticListener = "ActivityCreatedByHostingDiagnosticListener" ;
4041
42+ #region fetchers
43+
44+ // fetch is unique per event and per property
45+ private readonly PropertyFetcher httpContextFetcherStart = new PropertyFetcher ( "HttpContext" ) ;
46+ private readonly PropertyFetcher httpContextFetcherStop = new PropertyFetcher ( "HttpContext" ) ;
47+ private readonly PropertyFetcher httpContextFetcherBeginRequest = new PropertyFetcher ( "httpContext" ) ;
48+ private readonly PropertyFetcher httpContextFetcherEndRequest = new PropertyFetcher ( "httpContext" ) ;
49+ private readonly PropertyFetcher httpContextFetcherDiagExceptionUnhandled = new PropertyFetcher ( "httpContext" ) ;
50+ private readonly PropertyFetcher httpContextFetcherDiagExceptionHandled = new PropertyFetcher ( "httpContext" ) ;
51+ private readonly PropertyFetcher httpContextFetcherHostingExceptionUnhandled = new PropertyFetcher ( "httpContext" ) ;
52+ private readonly PropertyFetcher exceptionFetcherDiagExceptionUnhandled = new PropertyFetcher ( "exception" ) ;
53+ private readonly PropertyFetcher exceptionFetcherDiagExceptionHandled = new PropertyFetcher ( "exception" ) ;
54+ private readonly PropertyFetcher exceptionFetcherHostingExceptionUnhandled = new PropertyFetcher ( "exception" ) ;
55+
56+ private readonly PropertyFetcher timestampFetcherBeginRequest = new PropertyFetcher ( "timestamp" ) ;
57+ private readonly PropertyFetcher timestampFetcherEndRequest = new PropertyFetcher ( "timestamp" ) ;
58+ #endregion
59+
4160 /// <summary>
4261 /// Initializes a new instance of the <see cref="T:HostingDiagnosticListener"/> class.
4362 /// </summary>
@@ -72,19 +91,9 @@ public void OnSubscribe()
7291 /// <inheritdoc/>
7392 public string ListenerName { get ; } = "Microsoft.AspNetCore" ;
7493
75- /// <summary>
76- /// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.HttpRequestIn' event.
77- /// </summary>
78- [ DiagnosticName ( "Microsoft.AspNetCore.Hosting.HttpRequestIn" ) ]
79- public void OnHttpRequestIn ( )
80- {
81- // do nothing, just enable the diagnostic source
82- }
83-
8494 /// <summary>
8595 /// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.HttpRequestIn.Start' event.
8696 /// </summary>
87- [ DiagnosticName ( "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start" ) ]
8897 public void OnHttpRequestInStart ( HttpContext httpContext )
8998 {
9099 if ( this . client . IsEnabled ( ) )
@@ -176,7 +185,6 @@ public void OnHttpRequestInStart(HttpContext httpContext)
176185 /// <summary>
177186 /// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop' event.
178187 /// </summary>
179- [ DiagnosticName ( "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop" ) ]
180188 public void OnHttpRequestInStop ( HttpContext httpContext )
181189 {
182190 EndRequest ( httpContext , Stopwatch . GetTimestamp ( ) ) ;
@@ -185,7 +193,6 @@ public void OnHttpRequestInStop(HttpContext httpContext)
185193 /// <summary>
186194 /// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.BeginRequest' event.
187195 /// </summary>
188- [ DiagnosticName ( "Microsoft.AspNetCore.Hosting.BeginRequest" ) ]
189196 public void OnBeginRequest ( HttpContext httpContext , long timestamp )
190197 {
191198 if ( this . client . IsEnabled ( ) && ! this . enableNewDiagnosticEvents )
@@ -282,7 +289,6 @@ public void OnBeginRequest(HttpContext httpContext, long timestamp)
282289 /// <summary>
283290 /// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.EndRequest' event.
284291 /// </summary>
285- [ DiagnosticName ( "Microsoft.AspNetCore.Hosting.EndRequest" ) ]
286292 public void OnEndRequest ( HttpContext httpContext , long timestamp )
287293 {
288294 if ( ! this . enableNewDiagnosticEvents )
@@ -294,7 +300,6 @@ public void OnEndRequest(HttpContext httpContext, long timestamp)
294300 /// <summary>
295301 /// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.UnhandledException' event.
296302 /// </summary>
297- [ DiagnosticName ( "Microsoft.AspNetCore.Hosting.UnhandledException" ) ]
298303 public void OnHostingException ( HttpContext httpContext , Exception exception )
299304 {
300305 this . OnException ( httpContext , exception ) ;
@@ -310,7 +315,6 @@ public void OnHostingException(HttpContext httpContext, Exception exception)
310315 /// <summary>
311316 /// Diagnostic event handler method for 'Microsoft.AspNetCore.Hosting.HandledException' event.
312317 /// </summary>
313- [ DiagnosticName ( "Microsoft.AspNetCore.Diagnostics.HandledException" ) ]
314318 public void OnDiagnosticsHandledException ( HttpContext httpContext , Exception exception )
315319 {
316320 this . OnException ( httpContext , exception ) ;
@@ -319,7 +323,6 @@ public void OnDiagnosticsHandledException(HttpContext httpContext, Exception exc
319323 /// <summary>
320324 /// Diagnostic event handler method for 'Microsoft.AspNetCore.Diagnostics.UnhandledException' event.
321325 /// </summary>
322- [ DiagnosticName ( "Microsoft.AspNetCore.Diagnostics.UnhandledException" ) ]
323326 public void OnDiagnosticsUnhandledException ( HttpContext httpContext , Exception exception )
324327 {
325328 this . OnException ( httpContext , exception ) ;
@@ -559,5 +562,81 @@ public void Dispose()
559562 {
560563 SubscriptionManager . Detach ( this ) ;
561564 }
565+
566+ public void OnNext ( KeyValuePair < string , object > value )
567+ {
568+ HttpContext httpContext = null ;
569+ Exception exception = null ;
570+ long ? timestamp = null ;
571+
572+ switch ( value . Key )
573+ {
574+ case "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start" :
575+ httpContext = this . httpContextFetcherStart . Fetch ( value . Value ) as HttpContext ;
576+ if ( httpContext != null )
577+ {
578+ this . OnHttpRequestInStart ( httpContext ) ;
579+ }
580+ break ;
581+ case "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop" :
582+ httpContext = this . httpContextFetcherStop . Fetch ( value . Value ) as HttpContext ;
583+ if ( httpContext != null )
584+ {
585+ this . OnHttpRequestInStop ( httpContext ) ;
586+ }
587+ break ;
588+ case "Microsoft.AspNetCore.Hosting.BeginRequest" :
589+ httpContext = this . httpContextFetcherBeginRequest . Fetch ( value . Value ) as HttpContext ;
590+ timestamp = this . timestampFetcherBeginRequest . Fetch ( value . Value ) as long ? ;
591+ if ( httpContext != null && timestamp . HasValue )
592+ {
593+ this . OnBeginRequest ( httpContext , timestamp . Value ) ;
594+ }
595+ break ;
596+ case "Microsoft.AspNetCore.Hosting.EndRequest" :
597+ httpContext = this . httpContextFetcherEndRequest . Fetch ( value . Value ) as HttpContext ;
598+ timestamp = this . timestampFetcherEndRequest . Fetch ( value . Value ) as long ? ;
599+ if ( httpContext != null && timestamp . HasValue )
600+ {
601+ this . OnEndRequest ( httpContext , timestamp . Value ) ;
602+ }
603+ break ;
604+ case "Microsoft.AspNetCore.Diagnostics.UnhandledException" :
605+ httpContext = this . httpContextFetcherDiagExceptionUnhandled . Fetch ( value . Value ) as HttpContext ;
606+ exception = this . exceptionFetcherDiagExceptionUnhandled . Fetch ( value . Value ) as Exception ;
607+ if ( httpContext != null && exception != null )
608+ {
609+ this . OnDiagnosticsUnhandledException ( httpContext , exception ) ;
610+ }
611+ break ;
612+ case "Microsoft.AspNetCore.Diagnostics.HandledException" :
613+ httpContext = this . httpContextFetcherDiagExceptionHandled . Fetch ( value . Value ) as HttpContext ;
614+ exception = this . exceptionFetcherDiagExceptionHandled . Fetch ( value . Value ) as Exception ;
615+ if ( httpContext != null && exception != null )
616+ {
617+ this . OnDiagnosticsHandledException ( httpContext , exception ) ;
618+ }
619+ break ;
620+ case "Microsoft.AspNetCore.Hosting.UnhandledException" :
621+ httpContext = this . httpContextFetcherHostingExceptionUnhandled . Fetch ( value . Value ) as HttpContext ;
622+ exception = this . exceptionFetcherHostingExceptionUnhandled . Fetch ( value . Value ) as Exception ;
623+ if ( httpContext != null && exception != null )
624+ {
625+ this . OnHostingException ( httpContext , exception ) ;
626+ }
627+ break ;
628+ }
629+ }
630+
631+ /// <inheritdoc />
632+ public void OnError ( Exception error )
633+ {
634+ }
635+
636+ /// <inheritdoc />
637+ public void OnCompleted ( )
638+ {
639+ }
640+
562641 }
563642}
0 commit comments