asp.net - Global.asax 未处理的异常

标签 asp.net exception global-asax

我正在通过电子邮件发送来自 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/

相关文章:

javascript - d3.js scaleTime 返回未定义

Java 索引越界异常

使用自定义 Facebook 和 Google 按钮进行 ASP.NET Identity 登录

c# - 如何获得网络上可用的 SQL Server 列表?

Python:如何忽略异常并继续?

java - InvalidKeyException 加密-解密文件 - Java/Android

asp.net-mvc-4 - MVC 4 应用程序中的 ELMAH,在 Global.asax 中不起作用

nuget - 在 NuGet-Package Install 上修改 global.asax

c# - Global_Asax Application_Error Fired 而不是默认的 IIS7 404 Page Not Found Page

c# - 在 Reporting Services 2008 webservice 中调用渲染流时出错