ELMAH and API controller in MVC4 not logging errors

前端 未结 2 842
情书的邮戳
情书的邮戳 2021-02-05 20:09

Using an API controller in MVC4, when the controller action throws an exception, ELMAH does not log the error.

I think the problem is that MVC4 sets the HTTP status code

2条回答
  •  有刺的猬
    2021-02-05 20:59

    The anwser described before doesn't work. I've tried on my test server and received an error ("The given filter instance must implement one or more of the following filter interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.")

    Then I realized what happened .... you are trying to add the custom filter to the MVC Global Filter (filters.Add(new ElmahHandledErrorLoggerFilter());)

    To fix that, I've split the filter registration in GlobalFilter and HttpFilter

    FilterConfig.cs

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

    Global.asax

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        FilterConfig.RegisterHttpFilters(GlobalConfiguration.Configuration.Filters);
    

    ;-)

提交回复
热议问题