1、在Models中添加一個類MyExceptionAttribute,代碼如下
<code>namespace HSOA.MvcApp
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute());
//運行我們自己設置的過濾器
filters.Add(new MyExceptionAttribute());//註冊自定義的異常處理的過濾器
}
}
}/<code>
2、修改Global文件
<code>namespace HSOA.MvcApp.Controllers
{
public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
int a = 2;
int b = 0;
int c = a / b;
return Content(c.ToString());
}
}
}/<code>
3、將項目中App_Start文件中的代碼修改成我們的定義的代碼
<code>namespace HSOA.MvcApp.Controllers
{
public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
int a = 2;
int b = 0;
int c = a / b;
return Content(c.ToString());
}
}
}/<code>
4、寫一個測試程序
<code>namespace HSOA.MvcApp.Controllers
{
public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
int a = 2;
int b = 0;
int c = a / b;
return Content(c.ToString());
}
}
}/<code>
5、運行執行,在Log文件夾下已經有我們定義的一個錯誤日誌文件了
閱讀更多 恆臣 的文章