10

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?

1
  • It depends on what you want to log. You can put them in all of the handlers or none of them. Commented Jan 22, 2019 at 11:30

2 Answers 2

34
+50

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:

  1. ExceptionLogger
  2. ExceptionFilter
  3. 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.

  • Purpose: see all unhandled exceptions (before they even reach exception filters)

  • Scope: global


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

Sign up to request clarification or add additional context in comments.

2 Comments

I think we should use log framework in Exception loggers because from the document learn.microsoft.com/en-us/aspnet/web-api/overview/… - Exception loggers are the solution to seeing all unhandled exception caught by Web API. - Exception handlers are the solution for customizing all possible responses to unhandled exceptions caught by Web API. - Exception filters are the easiest solution for processing the subset unhandled exceptions related to a specific action or controller.
FYI, When the connection is about to be aborted and no new response message can be sent, the loggers will be called but the handler will not be called
0

If you want log the exceptions you have to put the code in your ExceptionHandler or Application_Error (It is same). ExceptionFilter is classifies the errors. Application_Error handles error if app is throws exception(s).

ExceptionFilter seperates and classifies errors via your error logic, ExceptionLogger logs when the error(s) occured as the same via your error logging logic.

In short you can put your code in Application_Error in global.asax

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.