How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

前端 未结 8 2074
后悔当初
后悔当初 2020-11-22 02:53

I am trying to use ELMAH to log errors in my ASP.NET MVC application, however when I use the [HandleError] attribute on my controllers ELMAH doesn\'t log any errors when the

8条回答
  •  野性不改
    2020-11-22 03:10

    Sorry, but I think the accepted answer is an overkill. All you need to do is this:

    public class ElmahHandledErrorLoggerFilter : IExceptionFilter
    {
        public void OnException (ExceptionContext context)
        {
            // Log only handled exceptions, because all other will be caught by ELMAH anyway.
            if (context.ExceptionHandled)
                ErrorSignal.FromCurrentContext().Raise(context.Exception);
        }
    }
    

    and then register it (order is important) in Global.asax.cs:

    public static void RegisterGlobalFilters (GlobalFilterCollection filters)
    {
        filters.Add(new ElmahHandledErrorLoggerFilter());
        filters.Add(new HandleErrorAttribute());
    }
    

提交回复
热议问题