views:

401

answers:

5

We'd like to just capture the YSOD output to use in an erorr reporting email, from a Global.asax error handler, for instance. Is there any way of leveraging the built-in ysod generator?

A: 

You should check out ELMAH it does what you are asking automatically.

Jeremy
+1  A: 

I would say in general, you do not want the user to experience the YSOD. This is something I've put in web apps before to capture the error and then allow for a more graceful error page to the user...

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
+1  A: 

Have you heard of ELMAH ? It might give you all the features you really want...

Here is a blog post that explains a bit about it : http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx

markt
+4  A: 

I would look into ELMAH (Error Logging Modules and Handlers for ASP.NET):

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.

Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code:

  • Logging of nearly all unhandled exceptions.
  • A web page to remotely view the entire log of recoded exceptions.
  • A web page to remotely view the full details of any one logged exception.
  • In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
  • An e-mail notification of each error at the time it occurs.
  • An RSS feed of the last 15 errors from the log.
Andrew Hare
+1  A: 

The Application_Error event in the Global.asax file is triggered whenever an unhandled exception occurs in the application. You can grab the last exception that occurred using the Server.GetLastError() method.

Similarly, you could create your own custom error page by specifying it in the web.config under the customErrors section in the web.config file. By specifying a default file, you can do any custom coding when an exception is routed there.

Dillie-O