How to log Application Errors when customErros is set to On?

天大地大妈咪最大 提交于 2019-12-22 01:09:29

问题


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>

回答1:


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?



来源:https://stackoverflow.com/questions/4290676/how-to-log-application-errors-when-customerros-is-set-to-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!