![](/img/trans.png)
[英]Active Directory fields in ASP.NET MVC4 - unhandled exception
[英]Catch Unhandled exception in mvc4
我如何在mvc4中捕獲未處理的異常,[不使用ELMAH]。 我嘗試了調用Application_Error
,但是可以記錄所有錯誤,包括找不到腳本和找不到圖像的路徑(不需要)。
可以排除的錯誤示例
找不到路徑'/Image/*.png'的控制器或未實現IController
找不到路徑'/~/Scripts/*.js'的控制器或未實現IController
碼
void Application_Error(object sender, EventArgs e)
{
Exception exc = Server.GetLastError();
//logging Exception (exc)
}
創建一個自定義錯誤處理程序,捕獲所需的內容(並記錄下來),然后忽略不想要的內容。
public class CustomHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (!(filterContext.Exception is InvalidOperationException))
{
// log
}
}
}
您將需要全局注冊錯誤處理程序。
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new CustomHandleErrorAttribute());
}
}
這些錯誤是未處理的異常。我認為您可以獲取此錯誤類型並忽略它進行記錄
protected void Application_Error()
{
var ex = Server.GetLastError();
if(check exceptiontype)
{}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.