How to log unhandled exception in .net core

16,584

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.

Share:
16,584

Related videos on Youtube

zubin joshi
Author by

zubin joshi

Updated on October 15, 2022

Comments

  • zubin joshi
    zubin joshi over 1 year

    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 ?

  • zubin joshi
    zubin joshi over 6 years
    Thanks Thomas, does it depend the order of registrations of Error logging and handling middleware ?
  • ThomasArdal
    ThomasArdal over 6 years
    If adding error logging middleware, you should add it after other pieces of middleware dealing with exceptions. You want your middleware to run as close to the thrown exception as possible, since other middleware may "swallow" exceptions.
  • Hamza Khanzada
    Hamza Khanzada about 4 years
    Just a quick question, Do I have to manually put try catch blocks everywhere and log exceptions to Elmahcore using HttpContext.RiseError()? or I would automatically catch thrown exceptions from my code once its configured?