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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
Method 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
Method 4
You should check out ELMAH it does what you are asking automatically.
Method 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("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6508002508004b060a08">[email protected]</a>"));
msg.From = new MailAddress("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c6a7e63614c6169226f6361">[email protected]</a>");
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");
}
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0