Can the ASP.NET Yellow Screen of Death (YSOD) be g

2019-02-05 09:05发布

问题:

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?

回答1:

Yes there is a way that you can do this.

In the Application_Error event in the global.asax file, get the last exception as an System.HttpUnhandledException. Your code will look like this:

var lastException = Server.GetLastError() as HttpUnhandledException;
string Ysod = lastException.GetHtmlErrorMessage();
// your call to emailing routine follows

I agree with other people's comments. You can also do this with ELMAH.



回答2:

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.


回答3:

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



回答4:

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



回答5:

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


回答6:

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.