Logging unhandled/fatal exception in .net core 8
I want to log fatal/unhandled exceptions to the db .
I used to work on .net framework and over there I used a combination of global.asax.cs (application_error) and log4net to achieve this.
Can someone kindly tell me hownit can be achieved in .net core 8 for a project that is a rest API.
I was looking into global error handling using
UseExceptionHandler.
But since I am new to .net core in general was feeling a bit confused.
Thanks in advance.
9 Replies
Exception handling middleware would be one way
I would have to dig into what are middlewares.
But I assume it is related to the UseExceptionHandler?
https://dev.to/andytechdev/aspnet-core-middleware-working-with-global-exception-handling-3hi0 here's a decent article on the topic
This one just uses regular middleware pipeline
.UseExceptionHandler()
also uses a middleware, IIRC
It basically captures the erorr, and redirects to some other route where you can handle it
Haven't used it with APIs, though, so can't say for sureThanks , I will look into the resource you shared.
Go for global exception handling.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-9.0 this also goes over the many options
Handle errors in ASP.NET Core
Discover how to handle errors in ASP.NET Core apps.
Can you please share a link or is it the same as the one above?
Sorry, I didn't read the replies. yes it is same.
Thanks a lot all, you guys are awesome