Unhandled Exceptions with Global.asax

為{幸葍}努か 提交于 2019-11-30 09:10:04

If this is a ASP.NET application, which the tag suggest it is, you should be able to do something like this... The ctx.Request.Url.ToString() would give you the file name of where the error occurred.

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");
}

In Visual Studio, you need to set the release build to generate debugging symbols. Unlike in the debug build, this isn't set by default. This will give you the full exception stack trace.

Even then, optimisations done by the JIT compiler (such as inlining) may mean that you don't get the right line number in your stack trace. If you want to be sure about the line number, you can also set the release build to "no optimisation". But this may mean that your app has lower performance and/or throughput (the latter tends tobe more important in a web app).

EDIT: You can find the "generate debugging symbols" setting by going to the Solution Explorer window, right-clicking on the project, and choosing the "Properties" menu item. Then go to Configuration Properties > Build > Generate Debugging Information and set to true or false. The Optimize Code setting is in the same window.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!