How to log unhandled exception in .net core

zubin joshi picture zubin joshi · Dec 6, 2017 · Viewed 13.6k times · Source

With ELMAH feature in web api 2.0 and Centralized logging and error handling , runtime calls logging module and decide if it can be handled then calls the handler else just logs it.. how can this feature be added in web api core. as we don't have inbuilt centralized unhandled logging feature..

One way I got is to use ExceptionHandler middleware and when it gets called, get the exception and log it or send email.. but what if handler doesn't get called .. how to log those unhandled exceptions ?

Answer

ThomasArdal picture ThomasArdal · Dec 6, 2017

Middleware could be a way to go yes. I've written a guide here: Error Logging Middleware in ASP.NET Core.

I would probably look at Microsoft.Extensions.Logging in combination with a logging framework like Serilog, NLog or log4net.