ASP.NET C# Error Notification and Flood Control

John Avis by | March 1, 2012 | ASP.NET Web Forms

It's a very good idea to have your websites email you with any server errors so you can be aware of any problems, whether they be something wrong with your code, or a problem with the server such as loss of connectivity with a database.

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

Related Posts

Bootstrap ASP.NET Web Forms

ASP.NET Web Forms and Bootstrap 3 Modals

by John Avis | July 6, 2017

There are a few methods for hiding and showing Bootstrap 3.x modals in an ASP.NET Web Forms application. This technique is pure ASP.NET (requires no client script) and allows you to show and hide modals by changing the Visible property of a containing PlaceHolder or Panel.


ASP.NET Web Forms

Disable common UpdateProgress controls for an UpdatePanel with an associated UpdateProgress

by John Avis | April 12, 2017

If you have an ASP.NET Web Forms page with multiple UpdatePanels and multiple UpdateProgress controls, sometimes you may want one or more UpdateProgress controls associated with a specific UpdatePanel, and one or more UpdateProgress controls that have no association with an UpdatePanel so are shown when any UpdatePanel is updated.


Bootstrap ASP.NET Web Forms

Bootstrap checkbox-inline and radio-inline with ASP.NET CheckBoxList and RadioButtonList controls

by John Avis | June 20, 2016

Although you can easily create inline checkboxes and radio buttons using ASP.NET CheckBoxes and RadioButtons using Bootstrap's checkbox-inline and radio-inline classes, it's not so easy with ASP.NET CheckBoxList and RadioButtonList controls.

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment
Tags
ASP.NET Html Forms ASP.NET MVC ASP.NET Web Forms ASP.NET Web Pages Bootstrap C# Classic ASP Cool Websites Databases eBay and PayPal Electrical Repairs General Hardware HTML/CSS Jquery/Javascript Media Center Mobile Phones Responsive Web Design SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP

About me

...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

Subscribe

Get the latest posts delivered to your inbox.