ASP.NET C# Error Notification and Flood Control

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

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Website Hosting Web Development

How to set up a website and database using the Turnkey Linux LAMP stack

by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).

Web Development

Intermittent "Unable to read data from the transport connection: net_io_connectionclosed" errors

by johna | May 6, 2020
If you are having intermittent problems sending email in .NET using System.Net.Mail consider switching libraries.

Comments

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

Leave a Comment

About

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

I recommend ASPnix for web hosting and Crazy Domains for domain registration.

Subscribe

Get the latest posts delivered to your inbox.