Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

handling Application_Error in ASP.NET app's global.asax

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...