A
A
Andrew2017-01-07 21:37:39
ASP.NET
Andrew, 2017-01-07 21:37:39

How to correctly catch all exceptions through the exception filter?

Hello!
I'm trying to catch all exceptions in web api controls
For this purpose, I wrote such a filter

public class NotImplExceptionFilterAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext filterContext)
        {
              // Вот тут записываем в лог информацию об исключении
        }
    }

The problem is that the exception itself is lost for some reason. The filterContext.Exception object always contains the following:
Message = Object reference not set to an instance of an object.
Data = System.Collections.ListDictionaryInternal
InnerException = 
TargetSite = Void Log(System.Web.Http.Filters.HttpActionExecutedContext)
StackTrace =    at MyApplication.Filters.WebApiLogFIlter.Log(HttpActionExecutedContext actionExecutedContext)
   at MyApplication.Filters.WebApiLogFIlter.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()
HelpLink = 
Source = MyApplication
HResult = -2147467261

What am I doing wrong and how do I get an exception in the exception filter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrDywar Pichugin, 2017-01-07
@byte916

1) Exception Filters
Using the actionExecutedContext.Exception property, we can get information about the exception that occurred in the controller method. You seem to have an exception in the filter itself, and the stack trace starts from there.
2) Global filters and filter redefinition
The second option for exceptions is more correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question