ELMAH - 过滤 404 错误

发布于 2024-09-02 05:51:33 字数 2624 浏览 2 评论 0原文

我正在尝试配置 ELMAH 来过滤 404 错误,但在使用 Web.config 文件中 XML 提供的过滤规则时遇到了困难。我按照此处此处 并添加了 声明在我的 ... 声明下,但完全失败了。

当我在本地测试它时,我在 Global.asax 的 void ErrorLog_Filtering() {} 中设置了一个断点,发现 ASP 触发了 System.Web.HttpException。 NET 的 404 似乎没有 System.IO.FileNotFound 的基本类型,而只是一个 System.Web.HttpException 。我通过在事件处理程序中调用 e.Exception.GetBaseException().GetType() 对此进行了测试。

接下来我决定尝试 希望任何异常消息匹配模式“文件'/foo.ext'不存在”将被过滤,但这也没有效果。作为最后的手段,我尝试了 ,甚至完全忽略了这一点。

我倾向于认为 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姐不稀罕 2024-09-09 05:51:33

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

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 所示:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文