ELMAH过滤不起作用

时间:2009-07-31 19:26:05

标签: asp.net filtering elmah

我的web.config中有以下设置:

<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>
    <security allowRemoteAccess="0" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnHere" />
</elmah>

<system.web>
    <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
        <add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
        <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />            
        <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules>
</system.web>

以下是我的global.asax文件:

public void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    Filter(e);
}

public void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
{
    Filter(e);
}

private void Filter(ExceptionFilterEventArgs e)
{
    var context = e.Context as HttpContext;

    if (context != null && context.Response.StatusCode == 404)
        e.Dismiss();

    if (e.Exception.GetBaseException() is FileNotFoundException ||
        e.Exception.GetBaseException() is HttpRequestValidationException)
        e.Dismiss();
}

然而,每次,Elmah都会记录404例外。我正在使用ASP.NET MVC;它们显示为System.Web.HttpException类型,而不是FileNotFound异常,但状态代码仍为404,因此过滤器应匹配,但它似乎根本不起作用。

我做错了什么?

1 个答案:

答案 0 :(得分:10)

找到答案。 Filter方法需要检查HttpException.GetHttpCode()方法的结果,而不是检查Response.StatusCode属性。

private void Filter(ExceptionFilterEventArgs e)
{
    var exception = e.Exception.GetBaseException();
    var httpException = exception as HttpException;

    if (httpException != null && 
        httpException.GetHttpCode() == 404)
        e.Dismiss();

    if (exception is FileNotFoundException ||
        exception is HttpRequestValidationException ||
        exception is HttpException)
        e.Dismiss();
}