View on GitHub


Web API development with Microsoft Azure in a breeze.

Logging unhandled exceptions


The ExceptionHandlingMiddleware class can be added to the ASP.NET Core pipeline to log unhandled exceptions that are thrown during request processing. The unhandled exceptions are caught by this middleware component and are logged through the ILogger implementations that are configured inside the project.


To use this middleware, add the following line of code in the Startup.Configure method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)


If you have multiple middleware components configured, it is advised to put the ExceptionHandlingMiddleware as the first one. By doing so, unhandled exceptions that might occur in other middleware components will also be logged by the ExceptionHandlingMiddleware component.

To get the exceptions logged in Azure Application Insights, configure logging like this when building the IWebHost in Program.cs:

       .ConfigureLogging(loggers => loggers.AddApplicationInsights())

To be able to use the AddApplicationInsights extension method, the Microsoft.Extensions.Logging.ApplicationInsights package must be installed.

If the parameter-less AddApplicationInsights method is used, the configurationsetting ApplicationInsights:TelemetryKey must be specified and the value of the telemetry-key of the Application Insights resource must be set.