However, when something goes seriously wrong you can be inundated with emails. For this reason it is a good idea to implement some sort of flood control on your error reporting.
Here's a method I use in my ASP.NET (C#) websites to send errors and handle flood control.
It works by recording the last error message received, and the time it was received. It also records an interval which is used to determine when it will allow the next same error message to be sent. In this case it won't allow a duplicate error email to be sent within 1 minute initially, then this interval grows to 5 minutes, then 10, 15, 30 and finally 60 minutes. This means that for a problem which is reoccuring after five emails in the first hour, you will only get one email an hour after that.
One limitation with with this sample is that if a different error occurs then it effectively resets the flood control. So if for some unusual reason different errors are occuring then you could receive each error.
An improvement to the sample would be to not to allow more than a certain number of emails to be sent within a specific time period.
void Application_Error(object sender, EventArgs e)
{
if (Request.Url.Host != "localhost")
{
HttpContext context = HttpContext.Current;
Exception err = (Exception)Server.GetLastError().InnerException;
int[] errorIntervals = { 1, 5, 10, 15, 30, 60 };
string lastError = Application["LastError"] == null ? string.Empty : (string)Application["LastError"];
int lastErrorInterval = Application["LastErrorInterval"] == null ? 0 : (int)Application["LastErrorInterval"];
DateTime lastErrorTime = Application["LastErrorTime"] == null ? DateTime.Now : (DateTime)Application["LastErrorTime"];
string thisError = err == null ? Server.GetLastError().Message : err.Message;
bool sendError = false;
if (lastError == thisError)
{
if (DateTime.Now > lastErrorTime.AddMinutes(errorIntervals[lastErrorInterval]))
{
Application["LastErrorInterval"] = lastErrorInterval == 5 ? 5 : lastErrorInterval + 1;
sendError = true;
}
}
else
{
Application["LastError"] = thisError;
Application["LastErrorInterval"] = 0;
sendError = true;
}
if (sendError)
{
Application["LastErrorTime"] = DateTime.Now;
string body = "<font face=verdana>"
+ "<h4><a href='" + context.Request.Url.ToString() + "'>" + context.Request.Url.ToString() + "</a></h4>"
+ (context.Request.UrlReferrer == null || context.Request.UrlReferrer.ToString() == string.Empty ? string.Empty : "<b>Refer Page: </b><a href='" + context.Request.UrlReferrer + "'>" + context.Request.UrlReferrer + "</a>");
if (err == null)
body += "<pre><font color='#002D62'>" + Server.GetLastError().Message + "</font></pre>"
+ "<p><b>Stack Trace: </b>" + Server.GetLastError().StackTrace + "</p>";
else
body += "<pre><font color='#002D62'>" + err.Message + "</font></pre>"
+ "<p><b>Stack Trace: </b>" + err.StackTrace + "</p>";
body += "<p><b>HTTP POST: </b>" + Request.Form.ToString() + "</p>"
+ "<p><b>User Agent: </b>" + context.Request.ServerVariables["HTTP_USER_AGENT"] + "</p>"
+ "<p><b>IP Address: </b>" + context.Request.ServerVariables["REMOTE_ADDR"] + "</p></font>";
MailMessage MyMailMessage = new MailMessage("Name", "mail@domain.com", "Server Error", body);
MyMailMessage.IsBodyHtml = true;
SmtpClient mailClient = new SmtpClient("server");
mailClient.Send(MyMailMessage);
}
}
}
Rate this post:
Comments
There are no comments yet. Be the first to leave a comment!