2013-03-01 53 views
2

我在全局asax中使用Application_Error事件來捕獲錯誤並將它們寫入文件。我可以看到在網頁中特定的異常,但在日誌文件中的所有我看到的是一個普通的錯誤是這樣的:在global.asax中捕獲特定錯誤

Exception of type 'System.Web.HttpUnhandledException' was thrown. 
    at System.Web.UI.Page.HandleError(Exception e) 
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
    at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
    at System.Web.UI.Page.ProcessRequest() 
    at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) 
    at System.Web.UI.Page.ProcessRequest(HttpContext context) 
    at ASP.review_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\...\ff7eee7c\ff24ade1\App_Web_tddyd4bt.3.cs:line 0 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

我怎麼能寫錯誤,我在黃色的頁面中看到日誌文件,而不是這個一般的錯誤?

回答

3

這是你在找什麼?

Exception ex = Server.GetLastError();  
String ExceptionMessage = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; 
0

在Global.asax中,找到的Application_Error方法,並添加以下內容:

protected void Application_Error(object sender, EventArgs e) 
    { 
     Exception ex = Server.GetLastError(); 
     LogHelper.WriteLog(ex); 
    } 

你需要創建一個類來寫日誌..

public class LogHelper 
{ 
    public static void WriteLog(Exception exception) 
    { 
     //write to text file the exception.Message ... 
    } 
}