I am frequently asked the best way to capture web application's unhandled exceptions that occur at a web host provider and without providing customers visibility to errors?
Answer:
The best way to capture web application bugs in a web host provider environment without providing error information to your users is to email the unhandled exception to a web administrator as follows:
Open the global.asax file in code view from the Solution Explorer.
Add the following code to the Application_Error() event handler in the global.asax file (update the below From and To email addresses with valid addresses):
protected void Application_Error( Object sender, EventArgs e)
{
if (System.Web.HttpContext.Current.Request.Url.Authority != "localhost")
{
// Get the Exception object wrapped in the InnerException property
Exception unhandledException =
(Exception)Server.GetLastError(). InnerException;
// Create a MailMessage
System.Web.Mail.MailMessage mail= new System.Web.Mail.MailMessage();
mail.From = "errors@server.com";
mail.To = "admin@server.com";
mail.BodyFormat = System.Web.Mail.MailFormat.Text;
mail.Subject = "unHandledException.Message";
mail.Body = unhandledException.ToString();
// Send the email
System.Web.Mail.SmtpMail.Send(mail);
// Redirect the user to a maintenence page
System.Web.HttpContext.Current.Response.Redirect("Maintenance.htm");
}
}
Create an HTM Page called "Maintenance.htm with the following syntax:
<HTML>
<body>
This page is currently being worked on by our technical support staff
and is unavailable. Please check back at a later time.
</body>
</HTML>
This code will e-mail exception details to the administrator and allow the user to know the page is being worked on. The exception will display as normal on the localhost.
Mike
- Cache entire web sites at the client and server and keep cache current. Cache just got easier!
enterprise library ( also known as microsoft patterns and practices) is a library of application blocks designed to assist developers with common enterprise development challenges
I usually follow a number of logging options other than emails. The problem with emails is that there may not be a guarantee of delivery for several reasons [spams / smtp servie issues etc]. You can consider using any of these options instead
(a) Write to a file. THere are issues with it as it requires access to file system. Also make sure you use a roll up mechanism so that the file does not overgrows.
(b) Save in the database. This makes it feasible to build up an interface to view error details.
(c) Post to event viewer.
Also, no matter what option you choose, consider using a utility like log4net which makes these options configurable.
MLibby
Participant
1530 Points
306 Posts
Capturing Unhandled Exceptions At A Web Host Provider
Jul 01, 2005 03:17 PM|LINK
I am frequently asked the best way to capture web application's unhandled exceptions that occur at a web host provider and without providing customers visibility to errors?
Answer:
The best way to capture web application bugs in a web host provider environment without providing error information to your users is to email the unhandled exception to a web administrator as follows:
protected void Application_Error( Object sender, EventArgs e)
{
if (System.Web.HttpContext.Current.Request.Url.Authority != "localhost")
{
// Get the Exception object wrapped in the InnerException property
Exception unhandledException =
(Exception)Server.GetLastError(). InnerException;
// Create a MailMessage
System.Web.Mail.MailMessage mail= new System.Web.Mail.MailMessage();
mail.From = "errors@server.com";
mail.To = "admin@server.com";
mail.BodyFormat = System.Web.Mail.MailFormat.Text;
mail.Subject = "unHandledException.Message";
mail.Body = unhandledException.ToString();
// Send the email
System.Web.Mail.SmtpMail.Send(mail);
// Redirect the user to a maintenence page
System.Web.HttpContext.Current.Response.Redirect("Maintenance.htm");
}
}
<HTML>
<body>
This page is currently being worked on by our technical support staff
and is unavailable. Please check back at a later time.
</body>
</HTML>
This code will e-mail exception details to the administrator and allow the user to know the page is being worked on. The exception will display as normal on the localhost.
Mike
Compcentric
Member
26 Points
27 Posts
Re: Capturing Unhandled Exceptions At A Web Host Provider
Jun 11, 2008 03:25 PM|LINK
I prefer to use the enterprise library to catch the exception and log to database.
Then I make a page in admin to present errors.
kemical
Participant
1211 Points
216 Posts
Re: Capturing Unhandled Exceptions At A Web Host Provider
Jun 19, 2008 03:14 PM|LINK
Hi
What’s the enterprise library? [*-)]
As I use a similar way to MLibby but also get user ip/details etc
Always interested in a different way[:)]
Remember to click “Mark as Answer” on the post if it helped you. Thank you!
Compcentric
Member
26 Points
27 Posts
Re: Capturing Unhandled Exceptions At A Web Host Provider
Jun 19, 2008 06:01 PM|LINK
enterprise library ( also known as microsoft patterns and practices) is a library of application blocks designed to assist developers with common enterprise development challenges
Here is a link:
http://msdn.microsoft.com/en-us/library/cc467894.aspx
kstyles662
Member
6 Points
5 Posts
Re: Capturing Unhandled Exceptions At A Web Host Provider
Jul 15, 2008 01:16 AM|LINK
This looks good.
thanks
Think about it.
sambeetpatra
Participant
1386 Points
227 Posts
Re: Capturing Unhandled Exceptions At A Web Host Provider
Jul 18, 2008 06:09 PM|LINK
I usually follow a number of logging options other than emails. The problem with emails is that there may not be a guarantee of delivery for several reasons [spams / smtp servie issues etc]. You can consider using any of these options instead
(a) Write to a file. THere are issues with it as it requires access to file system. Also make sure you use a roll up mechanism so that the file does not overgrows.
(b) Save in the database. This makes it feasible to build up an interface to view error details.
(c) Post to event viewer.
Also, no matter what option you choose, consider using a utility like log4net which makes these options configurable.