Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionFilter vs ExceptionLogger vs ExceptionHandler in Web API 2

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?

like image 893
Jack Avatar asked Jan 22 '19 11:01

Jack


People also ask

What are the different ways in which we can handle run time errors in Web API application code?

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.

How do I log exceptions in Web API?

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.

Which of the class is used for Web API error handling?

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.


1 Answers

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

like image 89
Andrew O. Avatar answered Oct 26 '22 15:10

Andrew O.