ELMAH - 过滤404错误

时间:2010-05-18 02:14:00

标签: asp.net filtering elmah

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

当我在本地测试它时,我在Global.asax的void ErrorLog_Filtering() {}中插入一个断点,发现ASP.NET为404发起的System.Web.HttpException似乎没有基础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>

1 个答案:

答案 0 :(得分:39)

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

ELMAH HttpModules的注册顺序是一个相关的问题,因为为了让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>

在再次审核ELMAH Wiki entry on ErrorFiltering后,我发现了一些信息,如果你问我,它确实值得一个&lt; strong /&gt; 标签。 (编辑:自从我第一次回答这个问题以来,他们已经加粗了;道具!)

  

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

相关问题