清除MVC 3.0应用程序中抛出的所有错误的方法? [英] Clean way to catch all errors thrown in an MVC 3.0 application?

查看:151
本文介绍了清除MVC 3.0应用程序中抛出的所有错误的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建一个MVC 3.0电子商务网站。我想避免在我的代码上粘贴try-catch块。



问题很简单 - 捕获我网站上抛出的所有错误的好策略是什么?我想知道他们所有的...所以我可以努力把这个计数值设置为0,一旦网站上线。我打算将每个错误写入记录器,并发送给它。



我已经在global.asax文件中完成了一些关于捕获它的阅读,但是我我们也读过它并没有得到他们所有的一切。



任何人都有任何建议或者可以指出我的方向很好?

解决方案

解决问题的MVC方法是过滤器。如果您已经在MVC3上,全球过滤器是为您而设。有特殊类型的过滤器用于处理错误: HandleError 过滤器。

您可以实现自己的过滤器并控制未处理的异常的各个方面,例如: p>

  public class HandleExceptionsAttribute:HandleErrorAttribute 
{
public override void OnException(ExceptionContext filterContext)
{
var expception = filterContext.Exception;

//自定义代码在这里..
}
}

最后,如果您希望存储和记录所有未处理的异常,请 ELMAH 即ELMAH.MVC是最好的选择。使用 NuGet软件包进行安装,并进行配置,所有数据将从以下链接访问:


http:// yourapp / admin / elmah /


我的建议是不是在Global.asax.cs中使用任何类似 Application_Error 的内容。


I'm building an MVC 3.0 ecommerce site. I want to avoid sticking try-catch blocks all over my code.

Question is pretty simple - What is a good strategy for capturing all errors that are thrown on my website? I want to know about all of them... so I can work on bringing that count to 0 once the site goes live. I plan on writing each error to a logger and also to email it.

I've done some reading about capturing it in the global.asax file... but i've also read that it doesn't get ALL of them.

Anyone have any suggestions or can point me in a good direction?

解决方案

The MVC way of solving that problem are filters. If you are on MVC3 already, global filters are for you. There is special type of filter for handing errors: the HandleError filter. This article describes much of using it.

You might implement you own filter and control every aspect of of unhandled exceptions, e.g.:

public class HandleExceptionsAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        var expception = filterContext.Exception;

        // custom code here..
    }
}

Finally, if you want to have all unhandled exceptions to be stored and logged, ELMAH i.e. ELMAH.MVC is the best choice. Install it by using the NuGet package, configure it and all data will be accessed from a link such as:

http://yourapp/admin/elmah/

My suggestion is to not use anything like Application_Error in Global.asax.cs.

这篇关于清除MVC 3.0应用程序中抛出的所有错误的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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