ELMAH - 过滤404错误

Nat*_*lor 23 asp.net filtering elmah

我正在尝试配置ELMAH来过滤404错误,我在Web.config文件中遇到了XML提供的过滤规则.我按照这里这里的教程,<is-type binding="BaseException" type="System.IO.FileNotFoundException" />在我的<test><or>...声明中添加了一个声明,但完全失败了.

当我在本地测试它时,我void ErrorLog_Filtering() {}在Global.asax中插入了一个断点,并发现System.Web.HttpExceptionASP.NET为404 启动的那个似乎没有基本类型System.IO.FileNotFound,而是它只是一个System.Web.HttpException.我通过调用e.Exception.GetBaseException().GetType()事件处理程序来测试它.

接下来我决定尝试一个<regex binding="BaseException.Message" pattern="The file '/[^']+' does not exist" />希望任何匹配模式"文件'/ foo.ext'不存在"的异常消息将被过滤,但这也没有效果.作为最后的手段我尝试过<is-type binding="BaseException" type="System.Exception" />,甚至完全无视.

我倾向于认为ELMAH存在配置错误,但我没有看到任何错误.我错过了一些明显的东西吗?

这是我的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>
Run Code Online (Sandbox Code Playgroud)

Nat*_*lor 39

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

注册ELMAH HttpModule的顺序是一个相关的问题,因为为了让ELMAH过滤异常,它必须首先知道哪些模块将消耗异常.必须最后注册ErrorFilter HttpModule以防止其他模块处理被过滤的异常.这对我来说似乎是一种倒退,但至少它有效.

<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>
Run Code Online (Sandbox Code Playgroud)

在再次回顾了关于ErrorFilteringELMAH Wiki条目后,我发现了这个小小的信息,如果你问我,它真的值得一个<strong />标签.(编辑:自从我第一次回答这个问题以来,他们已经加粗了;道具!)

第一步是配置一个名为Elmah.ErrorFilterModule的附加模块.确保在ELMAH的任何日志记录模块之后添加它,如下所示,使用ErrorLogModule: