ASP.NET C# Error Notification and Flood Control

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

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;
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>";
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", "", "Server Error", body);
MyMailMessage.IsBodyHtml = true;
SmtpClient mailClient = new SmtpClient("server");

Related Posts

Computers & Internet Web Development Website Hosting

500 Internal Server Error after migrating from IIS 7.5 to IIS 10

by John Avis | November 4, 2019

As support ends for Microsoft Windows Server 2008 I have recently gone through migrating some websites to a new server running Windows Server 2016 and IIS 10 but some of the websites did not work.

Jquery/Javascript Web Development

tagInput: A simple jQuery plugin for tag entry using Bootstrap 4

by John Avis | October 15, 2019

For a website project I needed a way to enter multiple tags. I just wanted something simple that I could easily modify to suit my own needs, so I wrote my own.

ASP.NET Web Forms Web Development

ASP.NET bug with RadioButton GroupName in Repeater

by John Avis | September 1, 2019

I rediscovered a bug in ASP.NET that affects RadioButtons inside repeaters. Here is my solution to the problem.


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

Leave a Comment


About me

...random postings about web development and programming, Internet, computers, electronics and automotive topics.


Get the latest posts delivered to your inbox.