in NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/DiagnosticListeners/Implementation/HostingDiagnosticListener.cs [333:424]
public void OnNext(KeyValuePair<string, object> value)
{
HttpContext httpContext = null;
Exception exception = null;
try
{
//// Top messages in if-else are the most often used messages.
//// Switch is compiled into GetHashCode() and binary search, if-else without GetHashCode()
//// is faster if 2.0 or higher events are used.
if (value.Key == "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start")
{
httpContext = this.httpContextFetcherStart.Fetch(value.Value) as HttpContext;
if (httpContext != null)
{
this.OnHttpRequestInStart(httpContext);
}
}
else if (value.Key == "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop")
{
httpContext = this.httpContextFetcherStop.Fetch(value.Value) as HttpContext;
if (httpContext != null)
{
this.OnHttpRequestInStop(httpContext);
}
}
else if (value.Key == "Microsoft.AspNetCore.Mvc.BeforeAction")
{
HttpContext context = null;
object routeData = null;
// Asp.Net Core 3.0 changed the field name to "RouteData" from "routeData and "HttpContext" from "httpContext"
if (this.aspNetCoreMajorVersion == AspNetCoreMajorVersion.Three)
{
context = this.httpContextFetcherOnBeforeAction30.Fetch(value.Value) as HttpContext;
if (context == null)
{
context = this.httpContextFetcherOnBeforeAction.Fetch(value.Value) as HttpContext;
}
routeData = this.routeDataFetcher30.Fetch(value.Value);
if (routeData == null)
{
routeData = this.routeDataFetcher.Fetch(value.Value);
}
}
else
{
context = this.httpContextFetcherOnBeforeAction.Fetch(value.Value) as HttpContext;
if (context == null)
{
context = this.httpContextFetcherOnBeforeAction30.Fetch(value.Value) as HttpContext;
}
routeData = this.routeDataFetcher.Fetch(value.Value);
if (routeData == null)
{
routeData = this.routeDataFetcher30.Fetch(value.Value);
}
}
var routeValues = this.routeValuesFetcher.Fetch(routeData) as IDictionary<string, object>;
if (context != null && routeValues != null)
{
OnBeforeAction(context, routeValues);
}
}
else if (this.trackExceptions && value.Key == "Microsoft.AspNetCore.Diagnostics.UnhandledException")
{
httpContext = this.httpContextFetcherDiagExceptionUnhandled.Fetch(value.Value) as HttpContext;
exception = this.exceptionFetcherDiagExceptionUnhandled.Fetch(value.Value) as Exception;
if (httpContext != null && exception != null)
{
this.OnDiagnosticsUnhandledException(httpContext, exception);
}
}
else if (this.trackExceptions && value.Key == "Microsoft.AspNetCore.Diagnostics.HandledException")
{
httpContext = this.httpContextFetcherDiagExceptionHandled.Fetch(value.Value) as HttpContext;
exception = this.exceptionFetcherDiagExceptionHandled.Fetch(value.Value) as Exception;
if (httpContext != null && exception != null)
{
this.OnDiagnosticsHandledException(httpContext, exception);
}
}
}
catch (Exception ex)
{
AspNetCoreEventSource.Instance.DiagnosticListenerWarning(value.Key, ex.ToInvariantString());
}
}