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

ASP.NET Web Forms Bootstrap

More on Bootstrap 4 modals in ASP.NET Web Forms applications using UpdatePanels

by John Avis | April 9, 2019

In this, my third post on this topic, I go over the methods I am using now and the problems I have encountered and overcome.

ASP.NET Web Forms Classic ASP

Custom error pages for ASP.NET Web Forms and Classic ASP in IIS 7 and 8.5

by John Avis | February 20, 2019

I was recently doing some work on a website which has a mixture of older Classic ASP pages and ASP.NET Web Forms pages and ran into problems with custom error pages.

ASP.NET Web Forms

Nicer localised UpdateProgress’ for your ASP.NET pages with multiple UpdatePanels

by John Avis | February 9, 2018

On an ASP.NET Web Forms page with multiple UpdatePanels it’s nice to give the user some feedback when one of the panels is being updated.


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

Leave a Comment
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 Reviews SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP Youtube

About me

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


Get the latest posts delivered to your inbox.