问题描述
我想发邮件给管理员,当应用程序崩溃。
所以,我只是做在的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());
}
但其实很多时候的Application_Error
被称为即使应用程序不会崩溃。
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.
推荐答案
当应用程序没有崩溃的发送什么样的错误?你可以检查异常的类型和不发送的电子邮件例外不崩溃的应用程序(例如重定向可以丢,我手动code滤波器的ThreadAbortException):
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.
您也可能会考虑与日志记录log4net的也可在服务器上记录错误的和的发送上的错误邮件。
Also you might look into logging with log4net which can also log errors on the server and send emails on errors.
这篇关于在ASP.NET应用程序的Global.asax中处理的Application_Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!