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:
- ExceptionLogger
- ExceptionFilter
- ExceptionHandler (if left not handled)
To better understand the difference between them, I will explain the purpose of each.
ExceptionLogger
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.
ExceptionFilter
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)
- Purpose: handle (not log) exceptions
- Scope: per action, per controller, global
ExceptionHandler
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".
- Purpose: log and handle unpredictable/unexpected exceptions
- Scope: global
I recommend to take a look at this article