How to log Application Errors when customErros is set to On?
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>Contro开发者_如何学Cller: </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>
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?
精彩评论