【发布时间】:2013-01-30 08:50:43
【问题描述】:
我有一个 Asp.Net 4.0 Web Forms 应用程序在某些时候会引发以下错误
“‘/MySiteDev’应用程序中的服务器错误”。
这个错误只是偶尔出现。而且此错误不会触发在Global.asax 中处理的Application_Error 事件。
既然这没有触发 Application_Error,那么还有哪些其他可能的地方会记录此错误事件的日志?除了事件查看器还有什么可用的?
有什么方法可以找出 ASP.Net 框架处理的异常?
注意:customErrors mode="Off"。还有runAllManagedModulesForAllRequests="true"
更新
来自How to: Handle Application-Level Errors的参考
在 Global.asax 文件中定义的错误处理程序将仅捕获在 ASP.NET 运行时处理请求期间发生的错误。例如,如果用户请求在您的应用程序中没有出现的 .aspx 文件,它将捕获错误。但是,如果用户请求不存在的 .htm 文件,它不会捕获错误。对于非 ASP.NET 错误,您可以在 Internet 信息服务 (IIS) 中创建自定义处理程序。服务器级错误也不会调用自定义处理程序。
您不能直接输出来自 Global.asax 文件的请求的错误信息;您必须将控制权转移到另一个页面,通常是 Web 窗体页面。将控制权转移到另一个页面时,使用 Transfer 方法。这会保留当前上下文,以便您可以从 GetLastError 方法获取错误信息。
处理错误后,必须通过调用Server对象(HttpServerUtility类)的ClearError方法来清除它。
代码
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);
}
配置
<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>
更新的参考文献
【问题讨论】: