gpt4 book ai didi

c# - 没有使用 Application_Error 记录错误

转载 作者:太空狗 更新时间:2023-10-29 20:31:09 24 4
gpt4 key购买 nike

我有一个 Asp.Net 4.0 Web Forms 应用程序在某些时候抛出以下错误

“Server Error in ‘/MySiteDev’ Application” .

这个错误只是偶尔出现。此错误不会触发在 Global.asax 中处理的 Application_Error 事件。

既然这不是触发 Application_Error,那么还有哪些其他可能的地方会有这个错误事件的日志?除了事件查看器之外还有其他可用的吗?

有什么方法可以找出 ASP.Net 框架处理的异常?

注意:customErrors mode="Off"。还有 runAllManagedModulesForAllRequests="true"

更新

引用自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);

}

配置

<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>

更新的引用资料

  1. Application_Error not firing
  2. Global.asax event Application_Error is not firing
  3. Application_Error does not fire?
  4. How to: Handle Application-Level Errors

最佳答案

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

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

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

关于c# - 没有使用 Application_Error 记录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14876752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com