捕获Global.asax中的错误

Peu*_*uge 12 c# asp.net error-handling

我的Global.aspx中有以下内容用于处理错误:

void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();
    if (exception != null)
    {
        //Log
        if (HttpContext.Current.Server != null)
        {
           HttpContext.Current.Server.Transfer("/siteerror.aspx");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这在很大程度上起作用,但有时不会进入Server.Transfer.由于某种原因,HttpContext.Current.Server为null.我想出了这种情况发生的地方:用户控件和业务逻辑类中发生错误.我在这里错过了什么吗?

谢谢

小智 1

应用程序错误可能随时发生 - 包括没有连接时。

例如,如果后台线程正在处理并导致异常。