2010-05-18 69 views
22

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

当我测试了它在当地我在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" />,希望任何匹配模式“The file'/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> 
+2

是第二个链接抛出一个404哦...反讽 – Baldy 2013-06-14 09:17:20

+0

@Baldy不幸的是,这是一个新的发展。你可以参考我的配置示例,了解你需要什么。 – 2013-06-19 17:20:28

+1

它的好纳森,这个话题很有帮助。我只是觉得有趣的是,这个话题是关于404s的,并且页面上的一个链接导致了404 :) – Baldy 2013-06-20 08:15:50

回答

38

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

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后再次我发现的信息 这个小珍闻这确实值得一 <强/ >标签,如果你问我 。 (编辑:他们已经粗体它,因为我第一次回答了这个问题;道具!)

的第一步是配置名为Elmah.ErrorFilterModule的附加模块。请确保您添加它之后的任何来自ELMAH日志模块,如这里ErrorLogModule所示: