ELMAH - 过滤404错误 [英] ELMAH - Filtering 404 Errors

查看:155
本文介绍了ELMAH - 过滤404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试配置ELMAH过滤404错误,我正在与我的Web.config文件中的XML提供的过滤规则的困难。我这里跟着教程和的这里,并增加了<是-类型绑定=BaseExceptionTYPE =System.IO.FileNotFoundException/> 在我的&LT声明;试验><或GT; ... 声明,但完全失败。

当我测试了它在当地我无效ErrorLog_Filtering(){} 在Global.asax,发现了的System.Web卡住断点.HttpException ,获取由ASP.NET为404解雇似乎并不具有 System.IO.FileNotFound 的基本类型,而是这是一个简单的 System.Web.HttpException 。我通过调用 e.Exception.GetBaseException()在事件处理程序测试此。的GetType()

接下来,我决定尝试一种<正则表达式绑定=BaseException.Message模式=文件/ [^'] +'不存在/> 在匹配模式中的任何异常信息文件'/foo.ext不存在的希望将得到过滤,但同样是有没有效果。作为最后一招我试过<是-类型绑定=BaseExceptionTYPE =System.Exception的/> ,甚至说完全忽略。

我倾向于认为有与ELMAH配置错误,但我看不出有什么。我缺少的东西公然明显?

下面是从我的web.config相关的东西:

 <结构>
  < configSections>
    < sectionGroup NAME =ELMAH>
      <节名称=安全requirePermission =假TYPE =Elmah.SecuritySectionHandler,ELMAH/>
      <节名称=错误日志requirePermission =假TYPE =Elmah.ErrorLogSectionHandler,ELMAH/>
      <节名称=errorMailrequirePermission =假TYPE =Elmah.ErrorMailSectionHandler,ELMAH/>
      <节名称=errorFilterrequirePermission =假TYPE =Elmah.ErrorFilterSectionHandler,ELMAH/>
    < / sectionGroup>
  < / configSections>
  <&ELMAH GT;
    < errorFilter>
      <试验>
        <或GT;
          <同等约束力=的HTTPStatus codeVALUE =404型=的Int32/>
          <正则表达式绑定=BaseException.Message模式=文件/ [^'] +'不存在/>
        &所述; /或GT;
      < /试验>
    < / errorFilter>
    <错误日志类型=Elmah.XmlFileErrorLog,ELMAH日志路径=〜/ App_Data文件/日志/ ELMAH/>
  < / ELMAH>
  <&的System.Web GT;
    <&的HttpModules GT;
      <添加名称=ErrorFilterTYPE =Elmah.ErrorFilterModule,ELMAH/>
      <添加名称=错误日志TYPE =Elmah.ErrorLogModule,ELMAH/>
    < /&的HttpModules GT;
  < /system.web>
  < system.webServer>
    <模块>
      <添加名称=ErrorFilterTYPE =Elmah.ErrorFilterModule,ELMAH/>
      <添加名称=错误日志TYPE =Elmah.ErrorLogModule,ELMAH/>
    < /模块>
  < /system.webServer>
< /结构>


解决方案

这的确是一个配置错误,只是没有一个特别明显。

在其中ELMAH的HttpModules注册的顺序是一个中肯的关注,因为为了ELMAH过滤异常就必须先知道哪些模块将消耗异常。 的ErrorFilter的HttpModule必须注册最后以prevent被过滤从处理异常的其他模块。这似乎[样]后退给我,但至少它的工作原理。

 <结构>
  ...
  <&的System.Web GT;
    <&的HttpModules GT;
      <添加名称=错误日志TYPE =Elmah.ErrorLogModule,ELMAH/>
      <添加名称=ErrorFilterTYPE =Elmah.ErrorFilterModule,ELMAH/>
    < /&的HttpModules GT;
  < /system.web>
  < system.webServer>
    <模块>
      <添加名称=错误日志TYPE =Elmah.ErrorLogModule,ELMAH/>
      <添加名称=ErrorFilterTYPE =Elmah.ErrorFilterModule,ELMAH/>
    < /模块>
  < /system.webServer>
< /结构>


审查后,<一个href=\"http://$c$c.google.com/p/elmah/wiki/ErrorFiltering#Filtering_Declaratively_via_Configuration\">ELMAH在ErrorFiltering维基条目再次我发现信息<击>这确实值得的&LT的这个小珍闻;强/&GT; 标签,如果你问我。 (修改:他们已经加粗它,因为我第一次回答了这个问题;道具)


  

的第一步是配置称为Elmah.ErrorFilterModule的附加模块。确保你添加后的任何从ELMAH日志模块,因为这里与ErrorLogModule图所示:


I am attempting to configure ELMAH to filter 404 errors and I am running into difficulties with the XML-provided filter rules in my Web.config file. I followed the tutorial here and here and added an <is-type binding="BaseException" type="System.IO.FileNotFoundException" /> declaration under my <test><or>... declaration, but that completely failed.

When I tested it locally I stuck a breakpoint in void ErrorLog_Filtering() {} of the Global.asax and found that the System.Web.HttpException that gets fired by ASP.NET for a 404 doesn't seem to have a base type of System.IO.FileNotFound, but rather it is simply a System.Web.HttpException. I tested this by calling e.Exception.GetBaseException().GetType() in the event handler.

Next I decided to try a <regex binding="BaseException.Message" pattern="The file '/[^']+' does not exist" /> in the hopes that any exception message matching the pattern "The file '/foo.ext' does not exist" would get filtered, but that too is having no effect. As a last resort I tried <is-type binding="BaseException" type="System.Exception" />, and even that is entirely disregarded.

I'm inclined to think there's a configuration error with ELMAH, but I fail to see any. Am I missing something blatantly obvious?

Here's the relevant stuff from my web.config:

<configuration>
  <configSections>
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
      <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
    </sectionGroup>
  </configSections>
  <elmah>
    <errorFilter>
      <test>
        <or>
          <equal binding="HttpStatusCode" value="404" type="Int32" />
          <regex binding="BaseException.Message" pattern="The file '/[^']+' does not exist" />
        </or>
      </test>
    </errorFilter>
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/logs/elmah" />
  </elmah>
  <system.web>
    <httpModules>
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
    </httpModules>
  </system.web>
  <system.webServer>
    <modules>
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
    </modules>
  </system.webServer>
</configuration>

解决方案

It was indeed a configuration error, just not one that was particularly obvious.

The order in which the ELMAH HttpModules are registered is a pertinent concern because in order for ELMAH to filter the exception it must first know which modules will be consuming the exception. The ErrorFilter HttpModule must be registered last to prevent the other modules from processing the exception being filtered. This seems [kind of] backwards to me, but at least it works.

<configuration>
  ...
  <system.web>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules>
  </system.web>
  <system.webServer>
    <modules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </modules>
  </system.webServer>
</configuration>


After reviewing the ELMAH Wiki entry on ErrorFiltering again I discovered this little tidbit of information which really deserves a <strong /> tag, if you ask me. (Edit: They've bolded it since I first answered this question; props!)

The first step is to configure an additional module called Elmah.ErrorFilterModule. Make sure you add it after any of the logging modules from ELMAH, as shown here with ErrorLogModule:

这篇关于ELMAH - 过滤404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆