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

当我在本地测试它时,我在其中设置了断点 void ErrorLog_Filtering() {} Global.asax 的结果发现 System.Web.HttpException 被 ASP.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>
有帮助吗?

解决方案

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

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>

审查后 有关 ErrorFiltering 的 ELMAH Wiki 条目 我再次发现了这一点小信息 这确实值得 <强/> 标签,如果你问我. (编辑: :自从我第一次回答这个问题以来,他们就把它加粗了;道具!)

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top