2

グローバルasaxでApplication_Errorイベントを使用して、エラーをキャッチし、ファイルに書き込みます。Webページで特定の例外を確認できますが、ログファイルで確認できるのは、次のような一般的なエラーだけです。

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)

この一般的なエラーの代わりに、イエローページに表示されているエラーをログファイルに書き込むにはどうすればよいですか?

4

2 に答える 2

3

これはあなたが探しているものですか?

 Exception ex = Server.GetLastError();    
 String ExceptionMessage = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
于 2013-03-01T19:21:15.370 に答える
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 ...
    }
}
于 2014-12-15T13:08:26.200 に答える