最好的方法是捕获服务器最后一个错误和 appdomain 异常。
所有这些都可以在 Global.asax.cs 文件中完成。
检查以下步骤:
1- 在 Global.asax.cs 中,捕获最后一个错误并记录它。
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Server.ClearError();
log.Error("Application Error caught in Global ", exception);
}
2-为 AppDomain 上的 UnhandledException 事件添加事件处理程序,这应该添加到 Application_Start :
protected void Application_Start(object sender, EventArgs e)
{
//....
AppDomain.CurrentDomain.UnhandledException
+= new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
3- 这是 CurrentDomain_UnhandledException 的实现:
void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
if (e != null)
log.Error("Domain Unhandled Exception: ", e.ExceptionObject as Exception);
}
快乐编码:)