I don't know about you, but when I am looking after a website I like to know if something has gone wrong on the website, mainly so you can fix it as soon as it happens and resolve the issue before the user of the website gets feed up and goes somewhere else.
Within ASP.NET you can configure the Global.ASAX Application_Error to capture an exceptions that are happening on you web site, so why not catch the exception and then e-mail it to the administrator of the website.
The example below works well with BlogEngine, as it uses the prebuilt settings and more importantly the Async sending of emails.
void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string err = "<b>Error Caught in Application_Error event</b><hr><br>" +
"<br><b>Error in: </b>" + Request.Url.ToString() +
"<br><b>Error Message: </b>" + objErr.Message.ToString() +
"<br><b>Stack Trace:</b><br>" +
objErr.StackTrace.ToString();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string from = BlogSettings.Instance.Email;
mail.To.Add(BlogSettings.Instance.Email);
mail.From = new System.Net.Mail.MailAddress(from, from);
mail.Subject = String.Format("Error in the Site - {0} Error", BlogSettings.Instance.Name);
mail.Body += String.Format("{0}<br/><br/><a href='{1}'>{1}</a><br/><br/>", System.DateTime.Now.ToString(), Request.Url.ToString());
mail.Body += "<b>Error Caught in Application_Error event</b><hr><br>" +
"<br><b>Error in: </b>" + Request.Url.ToString() +
"<br><b>Error Message: </b>" + objErr.Message.ToString() +
"<br><b>Stack Trace:</b><br>" + objErr.StackTrace.ToString();
mail.Body += "Server Variables<br/><br/>";
foreach (string x in Request.ServerVariables)
{
mail.Body += x + " - " + Request.ServerVariables[x] + "<br/>";
}
Utils.SendMailMessageAsync(mail);
}
Hi Bryan,
This is a good idea, but be careful when sending this kind of information by email.
If the errors occur a lot, you are going to get spammed badly with the same information.
I used to work for a company that used this idea. One day the mail server went down because 90,000 emails were generated due to the same error. The main problem with this solution is that you can’t control the frequency. Unfortunately they didn’t learn and a few months later they killed themselves again with around 75,000 emails.
The other issue with this is that email is not encrypted and you could be sending sensitive information about your site via email.
I would like to see this type of functionality rolled into BE, probably as an extension.
I agree with you 100%, they is very true and well worth mentioning. On larger scale more enterprise applications the exceptions could be logged to the event log and then the event log be monitored
The event log is definitely a better place for exceptions to be logged.
Here are a few things I have come across with regard to the event log. These are really for general interest because they really shouldn’t matter for your typical BE.Net usage. This is, as you say, more for enterprise level considerations.
– Event logs usually have size limits, which if reached, old records fall off the end of the log.
– A guy at work also found that the event log started to struggle at about 800 records/second on our test hardware
– SCOM is great for monitoring event logs, but it the event log agent doesn’t seem to look at records that already exist in the log when the agent starts (so you may miss some records).
🙂
Another way to monitor is using an external tool such as:
http://mon.itor.us
recommend to me by Chris Blankenship