asp.net-mvc MVC 4 全局异常过滤器如何实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13831933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC 4 Global Exception Filter how to implement?
提问by user1166305
How do I implement a global exception handler in MVC4 as it seems to be different from MVC3.
我如何在 MVC4 中实现全局异常处理程序,因为它似乎与 MVC3 不同。
Not sure how to implement the following:
不确定如何实现以下内容:
public class ErrorHandlerAttribute: System.Web.Mvc.FilterAttribute,
IExceptionFilter
{
public Task ExecuteExceptionFilterAsync(
HttpActionExecutedContext actionExecutedContext,
CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
回答by Thomas C. G. de Vilhena
Unfortunately the link provided in Eric Leschinski's commet only shows how to implement the System.Web.Mvc.IExceptionFilterinterface, and not the System.Web.Http.Filters.IExceptionFilterinterface. The first is used in regular MVC controllers, while the second targets ApiCotrollers.
不幸的是,Eric Leschinski 的 commet 中提供的链接仅显示了如何实现System.Web.Mvc.IExceptionFilter接口,而不是System.Web.Http.Filters.IExceptionFilter接口。第一个用于常规 MVC 控制器,而第二个用于 ApiCotrollers。
Here is a simple class example I came up with for logging unhandled exceptions thrown in my ApiControllers:
这是我想出的一个简单的类示例,用于记录在我的 ApiControllers 中抛出的未处理的异常:
public class ExceptionLoggerFilter: IExceptionFilter
{
public ExceptionLoggerFilter(Logger logger)
{
this.logger = logger;
}
public bool AllowMultiple { get { return true; } }
public Task ExecuteExceptionFilterAsync(
HttpActionExecutedContext actionExecutedContext,
CancellationToken cancellationToken)
{
return Task.Factory.StartNew(() =>
{
logger.Error("web service error", actionExecutedContext.Exception);
}, cancellationToken);
}
private Logger logger;
}
And all you have to do to enable this filter is register it in yours Global.asax Application_Start method:
启用此过滤器所需要做的就是在您的 Global.asax Application_Start 方法中注册它:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// allocate filter and add it to global configuration
var exceptionLogger = new ExceptionLoggerFilter(Container.Get<Logger>());
GlobalConfiguration.Configuration.Filters.Add(exceptionLogger);
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
I hope this helps other googlers out there!
我希望这可以帮助其他谷歌员工!
回答by ScubaSteve
The way I created an exception handler for MVC part of it, I created a class that implemented IExceptionFilter
我为它的 MVC 部分创建异常处理程序的方式,我创建了一个实现 IExceptionFilter 的类
public class MVCExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
Trace.TraceError(filterContext.Exception.ToString());
}
}
You then register it in the Global.asax.cs
inside protected void Application_Start()
你再把它注册在Global.asax.cs里面 protected void Application_Start()
The method already contains the line
该方法已包含该行
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
So, you will need to add this line ABOVEit
所以,你将需要添加这条线以上就
GlobalFilters.Filters.Add(new MVCExceptionFilter());

