【发布时间】:2011-02-20 16:53:27
【问题描述】:
我正在尝试将 ELMAH 配置为过滤 404 错误,但在我的 Web.config 文件中使用 XML 提供的过滤规则时遇到了困难。我按照教程here 和here 并在<test><or>... 声明下添加了<is-type binding="BaseException" type="System.IO.FileNotFoundException" /> 声明,但这完全失败了。
当我在本地测试它时,我在 Global.asax 的 void ErrorLog_Filtering() {} 中设置了一个断点,发现 ASP.NET 为 404 触发的 System.Web.HttpException 似乎没有 @987654327 的基本类型@,而是只是一个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>
【问题讨论】:
-
第二个链接抛出 404。哦,讽刺...
-
@Baldy 不幸的是,这是一个新的发展。您可以参考我的配置示例了解您的需要。
-
很好,内森,这个话题很有帮助。我只是觉得有趣的是这个主题是关于 404 的,页面上的一个链接指向 404 :)