Caveatrob
Caveatrob

Reputation: 13267

Block 404 FavIco error in ELMAh

I noticed out of the box that ELMAH logs a 404 not found for favico on my local server. How do I suppress this error through a filter? I'm not so familiar with configurating it yet..

Upvotes: 8

Views: 4385

Answers (6)

Nick Albrecht
Nick Albrecht

Reputation: 16928

I was able to get ELMAH to ignore the error by using the code below. You could potentially add any other paths you want to ignore as well. Technically these could be separate tests but on the off chance I want to ignore other 404 errors in the future that are related to my app, I figured I'd leave these grouped together since they are not dependent on my application whatsoever and are here solely to remove clutter from my error logging.

<errorFilter>
    <test>
        <or>
            <and>
                <equal binding="HttpStatusCode" value="404" type="Int32" />
                <equal binding="Context.Request.Path" value="/favicon.ico" type="string" />
            </and>
            <and>
                <equal binding="HttpStatusCode" value="404" type="Int32" />
                <equal binding="Context.Request.Path" value="/robots.txt" type="string" />
            </and>
        </or>
    </test>
</errorFilter>

Of course if you're worried about this cluttering your web.config you could always split the filters out to a dedicated file.

<elmah>
    <security allowRemoteAccess="1" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sqlserver" />
    <errorFilter configSource="elmahFilters.config" />
</elmah>

Upvotes: 5

Matt Frear
Matt Frear

Reputation: 54801

It's not that my web application is requesting a favicon, it's that when I browse to the elmah.axd page, the browser requests a favicon.

Supposedly, this should work:

<elmah>
  <errorFilter>
  <test>
      <and>
          <equal binding="HttpStatusCode" 
                 value="404" type="Int32" />
          <regex binding="Context.Request.ServerVariables['URL']" 
                 pattern="/favicon\.ico(\z|\?)" />
      </and>
  </test>
 </errorFilter>
</elmah>

But guess what, it doesn't.

The only way I've found to work is to add a favicon.ico to my web root. This is not for my site, it's only for the elmah.axd page. DO NOT need to call route.IgnoreRoute.

Here's elmah.axd with my favicon, and no errors:

elmah with favicon.ico

Upvotes: 4

Dan Ryan
Dan Ryan

Reputation: 406

I was using Kurt Schindler's programmatic solution above (vs config); however, I noticed that non-HttpExceptions were getting thrown twice in the ErrorLog_Filtering event handler. To avoid this, make sure you do a type-check on the GetBaseException method. Here's the snippet

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    if (e.Exception.GetBaseException() is HttpException)
    {
        if (((HttpException)e.Exception.GetBaseException()).GetHttpCode() == 404
            && ((HttpContext)e.Context).Request.Path == "/favicon.ico")
        {
            e.Dismiss();
        }
    }
}

Nothing new here, just an FYI if you happen to see some strange behavior.

Thanks...

Upvotes: 1

Rebecca
Rebecca

Reputation: 14402

i just ignore the route rather than configuring Elmah. This works for me:

routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});

Upvotes: 2

Kurt S
Kurt S

Reputation: 21357

The official elmah Error Filtering page explains a number of ways this 404 favicon error could be supressed.

You could filter out all 404 errors declaratively in the web.config like so. I'm not certain there is a way to only surpress a 404 for a favicon though.

<errorFilter>
    <test>
        <equal binding="HttpStatusCode" value="404" type="Int32" />
    </test>
</errorFilter>

If you wanted to do it programmatically, you could dismiss the error in the ErrorLog or ErrorEmail filtering events as explained in the official docs. The below code is a bit overkill, but it demonstrates how you could filter out only 404 errors for a /favicon.ico request.

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    if (((HttpException)e.Exception.GetBaseException()).GetHttpCode() == 404
       && ((HttpContext)e.Context).Request.Path == "/favicon.ico")
    {
        e.Dismiss();
    }
}

I'd personally prefer to either filter all 404s declaratively through the web.config, or just provide a favicon like Joel suggests.

Upvotes: 18

Joel Mueller
Joel Mueller

Reputation: 28745

It doesn't help you learn how to configure ELMAH, but the easiest way to prevent a 404 for requests for a favicon is to provide one...

Upvotes: 13

Related Questions