Get notified when your website throws an exception.

I used this code below in my website www.love2trade.com.

This will send an email whenever an exception is thrown in your asp.net website, there is a 15 minutes throttle on the frequency of emails.

In Application Start Class

private static int _exceptionCount;
private static DateTime _timeofLastException;
private static object _timeofLastExceptionLock = new object();
private static object _exceptionCountLock = new object();public static DateTime TimeofLastException
{
get { return _timeofLastException; }set { lock(_timeofLastExceptionLock){_timeofLastException = value;} }
}public static int ExceptionCount
{
get { return _exceptionCount; }set { lock(_exceptionCountLock){_exceptionCount = value;} }
}


In Global.asax
void Application_Error(object sender, EventArgs e){
Love2TradeHttpApplication.ExceptionCount++;
if(DateTime.Now > Love2TradeHttpApplication.TimeofLastException.Add(new TimeSpan(0,15,0)))
{
try
{
MailMessage mail = new MailMessage("errors@love2trade.com", "test@example.com");
string ErrorMessage = Love2TradeHttpApplication.ExceptionCount+
 " Exceptions were thrown since the last error email.rnThe error description is as follows : " + Server.GetLastError();
mail.Subject = "Exception thrown in love2trade.com";
mail.Priority = MailPriority.High;
mail.BodyEncoding = Encoding.ASCII;
mail.Body = ErrorMessage;
SmtpClient client = new SmtpClient();// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;client.Send(mail);
Love2TradeHttpApplication.TimeofLastException=DateTime.Now;
Love2TradeHttpApplication.ExceptionCount=0;
}
catch (Exception){
//donothing
}
}
}

Hope some will find this useful.-Ash

Advertisement

3 Responses

  1. There is alot more settings required than just the ones you have provided above.

    1. You have not stated how to name the (name)HttpAppllication
    2. There is no opening bracket for – DefaultNetworkCredentials;client.Sendmail);
    3. Are you supposed to put your smtp creedentials in these brackets or after the semi colon? SmtpClient client = new SmtpClient();// Add credentials if the SMTP server requires them.
    4. How do i create — In Application Start Class.
    5. and anything else that may get this working?

    Your help will be appreciated. Also we are getting close to open testing stages of our new ad serving product and we were wondering if you would be interested in hosting ads during our eveluation this of course is free. Not only will you have some attractive proffesional ads on your site you will also be able to have your ads on other websites (again free) and you will be able to view impression and click rates until the eveluation period is over and then you will have access to all features.

    Regards
    Regan.

  2. I’m sorry, I seem to have missed some important details.
    To answer your questions.
    1. Love2TradeHttpApplication class is simply a class i created that inherits from HttpApplication
    public class Love2TradeHttpApplication : HttpApplication
    I your global.asax you would need to instantiate an object of that class.

    ...In global.asax

    void Application_Start(Object sender, EventArgs e)
    {
    Love2TradeHttpApplication cha = new Love2TradeHttpApplication();
    cha.Application_Start(sender, e);
    //inside Application_Start you would call all startup code, start maintainance threads, load any necessary data from DB... etc
    }
    ...

    2. This is a typo and should actually be client.Send(mail);
    3. If you use SmtpClient client = new SmtpClient(); You are simply reading the server settings from the web.config file.
    You can also specify another server by doing

    SmtpClient client = new SmtpClient("smtpServerName");
    client.Credentials = new NetworkCredential("username", "password");
    client.Send(mail);

    4. Yes…look above

    -Regarding using your services, please contact me personally and let me know more about it.

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.