c# – Application_Error未记录错误

前端之家收集整理的这篇文章主要介绍了c# – Application_Error未记录错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Asp.Net 4.0 Web窗体应用程序,在某些时候会抛出以下错误

“Server Error in ‘/MySiteDev’ Application” .

错误有时会出现.并且此错误未触发在Global.asax中处理的Application_Error事件.

由于这不是触发Application_Error,所有其他可能的地方将有这个错误事件的日志?除了事件查看器以外还有什么吗?

有没有办法找出ASP.Net框架处理的异常?

注意:customErrors mode =“Off”.另外runAllManagedModulesForAllRequests =“true”

UPDATE

参考文献How to: Handle Application-Level Errors

An error handler that is defined in the Global.asax file will only catch errors that occur during processing of requests by the ASP.NET runtime. For example,it will catch the error if a user requests an .aspx file that does not occur in your application. However,it does not catch the error if a user requests a nonexistent .htm file. For non-ASP.NET errors,you can create a custom handler in Internet Information Services (IIS). The custom handler will also not be called for server-level errors.

You cannot directly output error information for requests from the Global.asax file; you must transfer control to another page,typically a Web Forms page. When transferring control to another page,use Transfer method. This preserves the current context so that you can get error information from the GetLastError method.

After handling an error,you must clear it by calling the ClearError method of the Server object (HttpServerUtility class).

protected void Application_Error(object sender,EventArgs e)
    {
        //Get the exception object
        Exception exception = Server.GetLastError().GetBaseException();

        //Get the location of the exception
        string location = Request.Url.ToString();
        if (!String.IsNullOrEmpty(location))
        {
            string[] partsOfLocation = location.Split('/');
            if (partsOfLocation != null)
            {
                if (partsOfLocation.Length > 0)
                {
                    location = partsOfLocation[partsOfLocation.Length - 1];
                }
            }

            //Maximum allowed length for location is 255
            if (location.Length > 255)
            {
                location = location.Substring(0,254);
            }
        }

        string connectionString = ConfigurationManager.ConnectionStrings[UIConstants.PayrollsqlConnection].ConnectionString;
        ExceptionBL exceptionBL = new ExceptionBL(connectionString);

        exceptionBL.SubmitException(exception.Message,location);
        Log.Logger.Error(exception.Message);

    }

CONFIG

<system.web>

<compilation debug="true" targetFramework="4.0" />
<pages validateRequest="false"></pages>
<httpRuntime requestValidationMode="2.0" />
<customErrors mode="Off"/>
<authentication mode="Windows"></authentication>
<identity impersonate="true" userName="domain\xxxx" password="xxxx"/>

</system.web>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpErrors errorMode="Detailed" />
</system.webServer>

更新的参考文献

> Application_Error not firing
> Global.asax event Application_Error is not firing
> Application_Error does not fire?
> How to: Handle Application-Level Errors

解决方法

检查事件查看器中的日志,该日志应记录服务器级别和应用程序级别的错误.

应用程序错误处理程序可能不处理事件,因为它是在应用程序成功启动并创建上下文之前发生的.因此,似乎应用程序配置或服务器配置停止处理请求.

或者,应用程序在请求生命周期中尽早遇到问题,即使在启动之后,它也会“挂起”,直到服务器决定终止进程(例如,可能以@MikeSmithDev提到的StackOverflowException的形式).

猜你在找的C#相关文章