Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ELMAH filtering not working

I have the following settings in my 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>

And the following in my global.asax file:

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();
}

And yet every single time, Elmah logs 404 exceptions. I'm using ASP.NET MVC; they show up as type System.Web.HttpException, not FileNotFound exception, but the status code is still 404 and so the filter should match, but it doesn't appear to be working at all.

What am I doing wrong?

like image 966
Chris Avatar asked Jul 31 '09 19:07

Chris


1 Answers

Found the answer. The Filter method needs to check the result of the HttpException.GetHttpCode() method, rather than checking the Response.StatusCode property.

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();
}
like image 179
Chris Avatar answered Sep 29 '22 15:09

Chris