当customErros设置为On时,如何记录应用程序错误?

时间:2010-11-27 08:05:54

标签: asp.net asp.net-mvc web-config custom-errors

我已经设置了一个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设置为“Off”时,我会看到死亡的黄色屏幕,并且可以在我的日志文件中看到错误。当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部分

<customErrors mode="On" defaultRedirect="~/error">
    <error statusCode="403" redirect="~/error"/>
    <error statusCode="404" redirect="~/error"/>
</customErrors>

1 个答案:

答案 0 :(得分:1)

customerrors函数只是在Application_Error方法之前处理它。您应该关闭customErrors并使用mvc的响应重定向将用户发送到相应的错误页面。

请参阅ASP.NET MVC Custom Error Handling Application_Error Global.asax?