ELMAH和API控制器MVC4不记录错误 [英] ELMAH and API controller in MVC4 not logging errors
问题描述
使用MVC4的API控制器,当控制器动作抛出一个异常,ELMAH不会记录错误。
Using an API controller in MVC4, when the controller action throws an exception, ELMAH does not log the error.
我认为问题在于MVC4设置HTTP状态code至500,它返回一个JSON对象异常的详细信息,但这么ELMAH永远看不到它,它不会引发未处理的异常。
I think the problem is that MVC4 sets the HTTP status code to 500, and it returns the exception details in a JSON object, but it does not throw an unhandled exception so ELMAH never sees it.
我怎样才能得到ELMAH捕捉到的所有响应的状态code是不是200?
How can I get ELMAH to capture all responses where the status code is not 200?
推荐答案
在描述不工作anwser。我已经尽我的测试服务器上,并收到一个错误(给定的过滤器实例必须实现以下过滤器接口中的一个或多个:个IAuthorizationFilter,IActionFilter,IResultFilter,个IExceptionFilter)
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.")
这时我才意识到发生了什么事....您要自定义过滤器添加到MVC全局筛选(filters.Add(新ElmahHandledErrorLoggerFilter());)
Then I realized what happened .... you are trying to add the custom filter to the MVC Global Filter (filters.Add(new ElmahHandledErrorLoggerFilter());)
要解决这个问题,我在GlobalFilter拆分过滤器注册和HttpFilter
To fix that, I've split the filter registration in GlobalFilter and HttpFilter
FilterConfig.cs
FilterConfig.cs
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterHttpFilters(HttpFilterCollection filters)
{
filters.Add(new ElmahHandledErrorLoggerFilter());
}
Global.asax中
Global.asax
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
FilterConfig.RegisterHttpFilters(GlobalConfiguration.Configuration.Filters);
; - )
这篇关于ELMAH和API控制器MVC4不记录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!