Deciding on implementation of ExceptionFilterAttribute OnException method

Viewed 697

I am trying to implement exception handling for Web Api using ExceptionFilterAttribute. I have inherited ExceptionFilterAttribute class and overridden the onException method.

public class ApiLogExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
        if (actionExecutedContext != null)
        {
            Logger.LogException(actionExecutedContext.Exception);
        }
    }
}

Lately, I have seen some implementation where the base class OnException method is also called in the overridden implementation.

public class ApiLogExceptionFilterAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext != null)
            {
                base.OnException(actionExecutedContext);
                Logger.LogException(actionExecutedContext.Exception);
            }
        }
    }

Which of the above two implementation is advisable? what is the use calling base method in this scenario?

1 Answers

I had the same question, so based on https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http/Filters/ExceptionFilterAttribute.cs (I hope it is current source of code) there is no point in calling base.OnException(actionExecutedContext);

Also from the nature of the filters, you can register multiple filters and you do not need inheritance for chaining. In theory you can call base just in case the current implementation changes and there is really anything done in the abstract base class. I am not calling it in my implementation.

Related