加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – ELMAH – 过滤404错误

发布时间:2020-12-15 18:42:56 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试配置ELMAH以过滤404错误,并且在我的Web.config文件中遇到XML提供的过滤器规则遇到困难。我遵循教程 here和 here,并添加了 is-type binding =“BaseException”type =“System.IO.FileNotFoundException”/声明在 test或 …声明下,但完全失败。
我正在尝试配置ELMAH以过滤404错误,并且在我的Web.config文件中遇到XML提供的过滤器规则遇到困难。我遵循教程 here和 here,并添加了< is-type binding =“BaseException”type =“System.IO.FileNotFoundException”/>声明在< test><或> …声明下,但完全失败。

当我在本地测试时,我在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 =“该文件'/ [^']'不存在”/>希望任何匹配模式“/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" />
    </modules>
  </system.webServer>
</configuration>

解决方法

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

注册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之后,我发现了这个小小的信息真的值得一个< strong />标签,如果你问我。 (编辑:自从我第一次回答这个问题以来,他们已经大胆了!道具!)

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:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读