views:

844

answers:

2

I am emailing unhandled exception details from global.asax. How can I get the path and/or filename of the aspx file or assembly file where an exception was not handled.

This info was showing up in the exception's stack trace when I was developing & testing. When I deployed the global.asax to production, this info is no longer showing up in the stack trace.

Is there a way to get to this info while I build my MailMessage object in Global.asax?

Thanks

A: 

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.

RoadWarrior
How is this done?
Ronnie Overby
+3  A: 

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("[email protected]"));
    msg.From = new MailAddress("[email protected]");
    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");
}
RSolberg
Awesome. Thanks!
Ronnie Overby
HttpContext.Current.Url gives you the current URL. But the question asked for the path and/or file name of the assembly.
RoadWarrior
@RoadWarrior: Since it is an ASP.NET application, chances are the URL is good enough. Since he accepted the answer, I'm guessing it meets his needs.
RSolberg