我正在通过电子邮件发送来自 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");
}
关于asp.net - Global.asax 未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/584199/