views:

554

answers:

3

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..

+2  A: 

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...

Joel Mueller
+3  A: 

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.

Kurt Schindler
This would do the job, but I wouldn't recommend filtering *all* 404 errors. That's kind of limitting the power of ELMAH. I'm more in favor of Joel's approach where you actually *fix* the problem.
senfo
+1  A: 

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

routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});
Junto
Are you sure you've actually checked this with ELMAH? Ignoring the route doesn't work. It just causes the request to be ignored by MVC and passed on to the regular ASP.NET pipeline which 404s it, causingit to be logged in ELMAH.
Mike Scott
It has been a while since I looked at that code, so I'd need to double check, but I used the technique on a live site and it appeared to work for me. Is it a difference between IIS6 and IIS7?
Junto