Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Exceptions with Global.asax

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

like image 230
Ronnie Overby Avatar asked Feb 24 '09 23:02

Ronnie Overby


People also ask

Which method is called in global ASAX to handle all unhandled exceptions?

An ExceptionFilterAttribute is used to collect unhandled exceptions. You can register it as a global filter, and it will function as a global exception handler.

What is unhandled exception in C#?

An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.

Can we run ASP.NET application without global ASAX file?

asax is not required by ASP.NET for a website to run. It is, however, very useful for application-level functionality (like unhandled exception logging). Save this answer.

Is global ASAX mandatory?

They defined methods for a single class, application class. They are optional, but a web application has no more than one global. asax file.


1 Answers

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");
}
like image 173
RSolberg Avatar answered Sep 22 '22 06:09

RSolberg