tags:

views:

1371

answers:

2

I wish to send mail to an administrator when the application crashes. So I simply do this in global.asax:

void Application_error(object sender, EventArgs e) { SendMessageToAdministarator(Server.GetLastError().ToString()); }

But actually many times Application_Error is called even though the application won't crash.

And I wish to send mail to admin ONLY when the application crashed.

Also, do I have a simple way to lift the application back on?

I'm looking for the simplest solution.

+6  A: 

What kind of errors are send when the application is not crashed? You could check the type of exception and don't send emails on the exceptions that don't crash the app (for example a redirect can throw the ThreadAbortException which I 'manually' filter in code):

protected void Application_Error(Object sender, EventArgs e)
{
 Exception ex = Server.GetLastError();
 if (ex is ThreadAbortException)
  return;

 Logger.Error(LoggerType.Global, ex, "Exception");
 Response.Redirect("unexpectederror.htm")
}

You could add a redirect to an error page with a message for the user that an error has occured and some links to relevant pages in the site. This is for the 'lift the application back on' - I hope this is what you wanted.

Also you might look into logging with log4net which can also log errors on the server and send emails on errors.

rslite
Thank you very much. What do you mean by "manually filter in code" ? also, one error that I constanly getting is some System.Web.HttpException: File does not exist. at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context)... but that doesn't do any noticeable harm
Hanan
I updated the answer with a code example. You can replace ThreadAbortException with HttpException. Or add multiple checks there for more exception types.
rslite
A: 

The ThreadAbotException due to Response.End() is not detected in Application_Error method. So how can we detect it in Application_Error?

Sandeep