开发者

How do I configure Log4Net to ignore 404 errors?

Http 404 errors are clogging up my log files. It's true I need to resolve the 404 errors, but doing so is a task in and of itself. I will use a tool like Fiddler for hunting down开发者_如何学JAVA the 404's but I do not want them logged.


a 404 is thrown as an instance of System.Web.HttpException. If you are trapping/logging exceptions in global.asax, something like this should do the trick:

protected void Application_Error( object sender , EventArgs e )
{
  Exception     exceptionInstance = Server.GetLastError() ;
  HttpException httpException     = exceptionInstance as HttpException ;
  int?          httpStatusCode    = ( httpException != null ? httpException.GetHttpCode() : (int?)null ) ;

  if ( !httpStatusCode.HasValue || httpStatusCode != 404 )
  {
    LogExceptionHere() ;
  }

  return ;

}

If you're using Elmah (and you should be), you need to configure it to ignore 404s in your web.config. Something along these lines:

<configuration>
  ...
  <elmah>
    ...
    <errorFilter>
      <test>
        <equal binding="HttpStatusCode" value="404" type="Int32" />
      </test>
    </errorFilter>
    ...
  </elmah>
  ...
</configuration>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜