How does ASP.NET Core know which middleware was executed for a request?
How does ASP.NET Core know which middleware was executed for a request? The first step is to add a nuget package reference Two Nuget packages need to be added: Microsoft.AspNetCore.MiddlewareAnalysis and Microsoft.Extensions.DiagnosticAdapter. The former is the core code implementation of the analysis and recording middleware and the latter is Used to receive log output. Since the DiagnosticSource method is used to record logs, you need to use the SubscribeWithAdapter method of the DiagnosticListener object to subscribe. The second step is to implement an analysis and diagnosis adapter This adapter is to facilitate us to output the log objects received from DiagnosticSource to the console. The specific code is implemented as follows public class AnalysisDiagnosticAdapter { private readonly ILogger _logger; public AnalysisDiagnosticAdapter(ILogger logger) { _logger = logger; } [DiagnosticName(“Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareStarting”)] public void OnMiddlewareStarting(HttpContext httpContext, string name, Guid instance, long timestamp) { _logger.LogInformation($”Middleware-Start: ‘{name}’; Request Path: ‘{httpContext.Request.Path}'”); } [DiagnosticName(“Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException”)] public void OnMiddlewareException(Exception exception, HttpContext httpContext, string name, Guid instance, long timestamp, long duration) { _logger.LogInformation($”Middleware-Exception: ‘{name}’; ‘{exception.Message}'”); } [DiagnosticName(“Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareFinished”)] public void OnMiddlewareFinished(HttpContext httpContext, string name, Guid instance, long timestamp, long duration) { _logger.LogInformation($”Middleware-End: Time-consuming [{duration/10000}] ‘{name}’; Status: ‘{httpContext.Response.StatusCode}'”); } } The third step is to register related services to enable the functions of…
How does ASP.NET Core know which middleware was executed for a request?
How does ASP.NET Core know which middleware was executed for a request? The first step is to add a nuget package reference Two Nuget packages need to be added: Microsoft.AspNetCore.MiddlewareAnalysis and Microsoft.Extensions.DiagnosticAdapter. The former is the core code implementation of the analysis and recording middleware and the latter is Used to receive log output. Since the DiagnosticSource method is used to record logs, you need to use the SubscribeWithAdapter method of the DiagnosticListener object to subscribe. The second step is to implement an analysis and diagnosis adapter This adapter is to facilitate us to output the log objects received from DiagnosticSource to the console. The specific code is implemented as follows public class AnalysisDiagnosticAdapter { private readonly ILogger _logger; public AnalysisDiagnosticAdapter(ILogger logger) { _logger = logger; } [DiagnosticName(“Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareStarting”)] public void OnMiddlewareStarting(HttpContext httpContext, string name, Guid instance, long timestamp) { _logger.LogInformation($”Middleware-Start: ‘{name}’; Request Path: ‘{httpContext.Request.Path}'”); } [DiagnosticName(“Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException”)] public void OnMiddlewareException(Exception exception, HttpContext httpContext, string name, Guid instance, long timestamp, long duration) { _logger.LogInformation($”Middleware-Exception: ‘{name}’; ‘{exception.Message}'”); } [DiagnosticName(“Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareFinished”)] public void OnMiddlewareFinished(HttpContext httpContext, string name, Guid instance, long timestamp, long duration) { _logger.LogInformation($”Middleware-End: Time-consuming [{duration/10000}] ‘{name}’; Status: ‘{httpContext.Response.StatusCode}'”); } } The third step is to register related services to enable the functions of…