MVC 4全球异常过滤器如何实现? [英] MVC 4 Global Exception Filter how to implement?

查看:140
本文介绍了MVC 4全球异常过滤器如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何实现MVC4一个全球性的异常处理程序,因为它似乎是从不同MVC3

不知道如何实现以下内容:

 公共类ErrorHandlerAttribute:System.Web.Mvc.FilterAttribute,
                                    个IExceptionFilter
{
    公共任务ExecuteExceptionFilterAsync(
            HttpActionExecutedContext actionExecutedContext,
            的CancellationToken的CancellationToken)
    {
        抛出新NotImplementedException();
    }
}


解决方案

不幸的是埃里克Leschinski的commet提供的链接只显示了如何实现<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.iexceptionfilter%28v=vs.108%29.aspx\">System.Web.Mvc.IExceptionFilter接口,而不是<一href=\"http://msdn.microsoft.com/pt-br/library/system.web.http.filters.iexceptionfilter%28v=vs.108%29.aspx\">System.Web.Http.Filters.IExceptionFilter接口。第一种是在常规MVC控制器使用,而第二靶材<一href=\"http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api\">ApiCotrollers.

下面是我想出了登录我ApiControllers抛出未处理的异常的简单类的例子:

 公共类ExceptionLoggerFilter:个IExceptionFilter
{
    公共ExceptionLoggerFilter(记录器记录器)
    {
        this.logger =记录仪;
    }    公共BOOL的AllowMultiple {获得{返回true; }}    公共任务ExecuteExceptionFilterAsync(
            HttpActionExecutedContext actionExecutedContext,
            的CancellationToken的CancellationToken)
    {
        返回Task.Factory.StartNew(()=&GT;
        {
            logger.Error(Web服务的错误,actionExecutedContext.Exception);
        }的CancellationToken);
    }    私人记录仪记录;
}

和所有你需要做的,以使该过滤器是在你的Global.asax Application_Start方法中注册它:

 保护无效的Application_Start()
{
    AreaRegistration.RegisterAllAreas();    //分配过滤,并将其添加到全局配置
    VAR exceptionLogger =新ExceptionLoggerFilter(Container.Get&LT;&记录仪GT;());
    GlobalConfiguration.Configuration.Filters.Add(exceptionLogger);    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

我希望这可以帮助其他的Google那里!

How do I implement a global exception handler in MVC4 as it seems to be different from 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();
    }
}

解决方案

Unfortunately the link provided in Eric Leschinski's commet only shows how to implement the System.Web.Mvc.IExceptionFilter interface, and not the System.Web.Http.Filters.IExceptionFilter interface. The first is used in regular MVC controllers, while the second targets ApiCotrollers.

Here is a simple class example I came up with for logging unhandled exceptions thrown in my 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:

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!

这篇关于MVC 4全球异常过滤器如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆