View on GitHub

arcus.webapi

Web API development with Microsoft Azure in a breeze.

Logging unhandled exceptions

Introduction

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.

Usage

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

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   app.UseMiddleware<Arcus.WebApi.Logging.ExceptionHandlingMiddleware>();

   ...
   app.UseMvc();
}

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:

WebHost.CreateDefaultBuilder(args)
       .UseApplicationInsights()
       .ConfigureLogging(loggers => loggers.AddApplicationInsights())
       .UseStartup<Startup>();

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.