Henrik Fransas
Mar 21, 2014
  5385
(1 votes)

How to remove logging of 404-request from EPiServers logfiles

When a user request a file or url that does not exist in a EPiServer-site they will get a 404 response back and that is fine. The problem is that EPiServer also report this as a Error to Log4Net with the error “1.2.5 Unhandled exception in ASP.NET System.Web.HttpException (0x80004005): Not Found. at EPiServer.Web.StaticFileHandler.ProcessRequest(HttpContext context)”.

This is no problem for the end user since they are getting the right response but for the developers of the site it can lead to problems when they are looking for errors in the site and all they see is 404’s in the log. I have tried to solve this for a while, I even have a support ticket open with EPiServer support on how to be able to not log this and have not got or find any good ways until right now. Luckily I find a answer on stackowerflow on why “log4net.Filter.StringMatchFilter” not working with errors (it only locks in the message, not in the error description). Read it here: http://goo.gl/U3zBJn

To solve this I did like this:
First create a class named: ExceptionMessageToMatchFilter

using System;
using log4net.Core;
using log4net.Filter;

namespace EPiServer.Templates.Alloy.Business.Core
{
    public class ExceptionMessageToMatchFilter : StringMatchFilter
    {
        public override FilterDecision Decide(LoggingEvent loggingEvent)
        {
            if (loggingEvent == null)
                throw new ArgumentNullException("loggingEvent");

            if (loggingEvent.ExceptionObject == null)
                return FilterDecision.Neutral;

            var exceptionMessage = loggingEvent.GetExceptionString();

            if (m_regexToMatch != null)
            {
                if (!m_regexToMatch.Match(exceptionMessage).Success)
                    return FilterDecision.Neutral;

                return m_acceptOnMatch ? FilterDecision.Accept : FilterDecision.Deny;
            }

            if (m_stringToMatch == null || exceptionMessage.IndexOf(m_stringToMatch) == -1)
            {
                return FilterDecision.Neutral;
            }

            return m_acceptOnMatch ? FilterDecision.Accept : FilterDecision.Deny;
        }
    }
}

After that set up a filter in your EPiServerLog.config on the appender you like to be filtered. In this example I filter away both files/friendly url’s and also url’s with a aspx-extension. When you add the filter set the path attribute to the path to the class (EPiServer.Templates.Alloy.Business.Core) and then a comma and the name of the assambly the class is in (EPiServer.Templates.Alloy).

<?xml version="1.0" encoding="utf-8"?>
<log4net>
    <appender name="errorFileLogAppender" type="log4net.Appender.RollingFileAppender" >
        <!-- Consider moving the log files to a location outside the web application -->
        <file value="App_Data\EPiServerErrors.log" />
        <encoding value="utf-8" />
        <staticLogFileName value="true"/>
        <datePattern value=".yyyyMMdd.'log'" />
        <rollingStyle value="Date" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %level %logger: %message%n" />
        </layout>
      <filter type="EPiServer.Templates.Alloy.Business.Core.ExceptionMessageToMatchFilter, EPiServer.Templates.Alloy" >
        <stringToMatch value="System.Web.HttpException (0x80004005): Not Found" />
        <acceptOnMatch value="false" />
      </filter>
      <filter type="EPiServer.Templates.Alloy.Business.Core.ExceptionMessageToMatchFilter, EPiServer.Templates.Alloy" >
        <regexToMatch value="System.Web.HttpException \(0x80004005\): The file '[^']*' does not exist\." />
        <acceptOnMatch value="false" />
      </filter>
    </appender>
    <appender name="outputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" >
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="[%thread] %level %logger: %message%n" />
        </layout>
    </appender>

    <root>
        <level value="warn" />
        <appender-ref ref="errorFileLogAppender" />
    </root>
</log4net>

Save, compile and voila then you do not have any 404-requests in the log file anymore!

Happy coding!

Henrik Fransas

Mar 21, 2014

Comments

Janne Kuusela Kuusela
Janne Kuusela Kuusela May 23, 2014 01:29 PM

Hi,

i was trying to implement this but can't get it to work. Something wrong with the custom/extended version of StringMatchFilter. I enabled log4net logging and this is what I get:

object type [Customer.Site.Business.Logging.ExceptionMessageToMatchFilter] is not assignable to type [log4net.Filter.IFilter]. There are no acceptable type conversions.

Any idea why? I basically copied your solution. Here is the filter config:










Henrik Fransas
Henrik Fransas May 23, 2014 02:27 PM

Looks like you are using the same namespace that I do in the example, probably your class is in another namespace

Justin Le
Justin Le Jun 20, 2014 06:38 AM

Work like charm! Thank you very much Henrik, you save my day!

Please login to comment.
Latest blogs
Plug-in manager is back in CMS 12

Plug-in manager is back in the UI, what is it and how can i use it?

Luc Gosso (MVP) | Oct 6, 2022 | Syndicated blog

Display Child Pages in Content Delivery API Response

The below example will implement an instance of IContentConverterProvider to customise the serialisation of PageData and output child pages in the...

Minesh Shah (Netcel) | Oct 4, 2022

Bring the Report Center back in Optimizely CMS 12

The Report Center has been a part of Optimizely CMS since its first debut in version 5R2 in 2008, but in CMS 12, it's removed! Don't despair! Make...

Tomas Hensrud Gulla | Oct 4, 2022 | Syndicated blog

Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Optimizely names Luminary Senior Developer, Ynze Nunnink, OMVP

Luminary Senior Developer and Optimizely Lead, Ynze Nunnink has secured the coveted position of Optimizely MVP. Earning a Platinum badge for his...

Ynze | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog