I was reading this article and there seems to be three different exception events.
I'd like to use a logging framework like Serilog to capture exceptions.
Should I be putting logging code in ExceptionFilter
, then ExceptionLogger
, then ExceptionHandler
? I assume they all have access to the full exception stack.
Additionally, should I also put logging code in Application_Error
in the global.asax
?
You can customize how Web API handles exceptions by writing an exception filter. An exception filter is executed when a controller method throws any unhandled exception that is not an HttpResponseException exception.
Today there's no easy way in Web API to log or handle errors globally. Some unhandled exceptions can be processed via exception filters, but there are a number of cases that exception filters can't handle. For example: Exceptions thrown from controller constructors.
Using HttpResponseException in ASP.NET Web API You can use the HttpResponseException class to return specific HTTP status codes and messages from your controller methods in Web API. Here is an example. If your Web API returns IHttpActionResult, you might want to write the GetEmployee method as shown below.
Apparently, you have a need to log all unhanded exceptions for monitoring overall health of your application. For this purpose using ExceptionHandler would be your best option.
If an unhanded exception is thrown, it will be received by the following in the same order:
To better understand the difference between them, I will explain the purpose of each.
Solution to seeing all unhanded exceptions caught by Web API. There is a chance of an exception being thrown before you even hit controller's action or after controller already returned a action result response (remember, return value from an action doesn't go directly to the browser, but is handled further by the request/response pipeline).
For example, an exception may occur in controller's constructor, during routing, response serialization etc
Multiple exception loggers may be registered and all of them will be called in case of exception.
Be aware that exception loggers are called before exception filters, where they will be handled, so you may get a lot of irrelevant information if logging these.
Purpose: see all unhandled exceptions (before they even reach exception filters)
Scope: global
Use this one for handling expected exceptions (e.g. UnauthorizedException thrown by your business logic) and customize response based on type of exception. In other words, use it for acting on an exception (e.g. if exception is UnathorizedException then redirect user to login page).
First filter to handle exception "wins", so that other exception handlers wont even be called if exception was already handled (response object is set)
This one can and should be used for logging of unhanded exceptions. Only one can be registered per application. It can also be used to show a generic error page to the user - "An unknown error has occured".
I recommend to take a look at this article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With