Asp.net

Global.asax 的未處理異常

  • February 26, 2009

我正在從 global.asax 通過電子郵件發送未處理的異常詳細資訊。如何獲取未處理異常的 aspx 文件或程序集文件的路徑和/或文件名。

當我開發和測試時,此資訊顯示在異常的堆棧跟踪中。當我將 global.asax 部署到生產環境時,此資訊不再顯示在堆棧跟踪中。

當我在 Global.asax 中建構 MailMessage 對象時,有沒有辦法獲取此資訊?

謝謝

如果這是標籤所暗示的 ASP.NET 應用程序,您應該能夠執行類似的操作… ctx.Request.Url.ToString() 將為您提供錯誤發生位置的文件名。

protected void Application_Error(object sender, EventArgs e)
{
   MailMessage msg = new MailMessage();
   HttpContext ctx = HttpContext.Current;

   msg.To.Add(new MailAddress("me@me.com"));
   msg.From = new MailAddress("from@me.com");
   msg.Subject = "My app had an issue...";
   msg.Priority = MailPriority.High;

   StringBuilder sb = new StringBuilder();
   sb.Append(ctx.Request.Url.ToString() + System.Environment.NewLine);
   sb.Append("Source:" + System.Environment.NewLine + ctx.Server.GetLastError().Source.ToString());
   sb.Append("Message:" + System.Environment.NewLine + ctx.Server.GetLastError().Message.ToString());
   sb.Append("Stack Trace:" + System.Environment.NewLine + ctx.Server.GetLastError().StackTrace.ToString());
   msg.Body = sb.ToString();

   //CONFIGURE SMTP OBJECT
   SmtpClient smtp = new SmtpClient("myhost");

   //SEND EMAIL
   smtp.Send(msg);

   //REDIRECT USER TO ERROR PAGE
   Server.Transfer("~/ErrorPage.aspx");
}

引用自:https://stackoverflow.com/questions/584199