【问题标题】:catching specific error in global.asax在 global.asax 中捕获特定错误
【发布时间】:2013-02-16 09:23:42
【问题描述】:

我在全局 asax 中使用 Application_Error 事件来捕获错误并将它们写入文件。我可以在网页中看到特定的异常,但在日志文件中,我看到的只是这样的一般错误:

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)

如何将我在黄页中看到的错误写入日志文件而不是这个一般错误?

【问题讨论】:

    标签: c# asp.net exception-handling global-asax


    【解决方案1】:

    这是你要找的吗?

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

    【讨论】:

      【解决方案2】:

      在 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 ...
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-25
        • 2012-04-21
        • 2019-02-20
        • 2015-04-30
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 2011-11-05
        相关资源
        最近更新 更多