Answer the question
In order to leave comments, you need to log in
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)
{
// Вот тут записываем в лог информацию об исключении
}
}
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question