当 customErros 设置为 On 时如何记录应用程序错误?
我已经设置了一个 MVC 应用程序来使用 log4net 记录错误。在我的 global.asax 中,我使用 Application_Error 方法捕获应用程序错误(s_log 是我的日志记录管理器,它是 Log4net 的包装器)。
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
s_log.LogException(true, ex, "Application Error");
}
当 customErrors 设置为“关闭”时,我会出现黄屏死机,并且可以在日志文件中看到错误。当 customErrors 设置为“On”时,我会看到错误页面(以及详细的错误),但日志文件中未写入任何内容(调试不会遇到 Application_Error)。
有人可以告诉我为什么会发生这种情况吗?
Error.aspx
<h1>Sorry, an error occurred while processing your request.</h1>
<strong>Controller: </strong> <%= Model.ControllerName %><br />
<strong>Action: </strong> <%= Model.ActionName %>
<% if (Model.Exception != null) { %>
<p>
<strong>Exception Details:</strong><br />
<%= Model.Exception.ToString() %>
</p>
<% } %>
web.config 中的自定义错误部分
<customErrors mode="On" defaultRedirect="~/error">
<error statusCode="403" redirect="~/error"/>
<error statusCode="404" redirect="~/error"/>
</customErrors>
I have setup an MVC application to log errors with log4net. In my global.asax I catch Application errors using the Application_Error method (s_log is my logging manager which is a wrapper around Log4net).
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
s_log.LogException(true, ex, "Application Error");
}
When customErrors is set to "Off" I get the yellow screen of death and can see the error in my log file. When customErrors is set to "On" I see my Error page (and the detailed error) but nothing is written in the log file (debugging doesn't hit the Application_Error).
Can someone enlighten me on why this happens?
Error.aspx
<h1>Sorry, an error occurred while processing your request.</h1>
<strong>Controller: </strong> <%= Model.ControllerName %><br />
<strong>Action: </strong> <%= Model.ActionName %>
<% if (Model.Exception != null) { %>
<p>
<strong>Exception Details:</strong><br />
<%= Model.Exception.ToString() %>
</p>
<% } %>
customErrors section in web.config
<customErrors mode="On" defaultRedirect="~/error">
<error statusCode="403" redirect="~/error"/>
<error statusCode="404" redirect="~/error"/>
</customErrors>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
customerrors 函数只是在 Application_Error 方法之前处理它。您应该关闭 customErrors 并使用 mvc 的响应重定向将用户发送到适当的错误页面。
请参阅 ASP.NET MVC 自定义错误处理 Application_Error Global.asax ?
The customerrors function is just taking care of it before the Application_Error method can. You should just keep customErrors off and use mvc's response redirect to send user's to the appropriate error page.
See ASP.NET MVC Custom Error Handling Application_Error Global.asax?