Sitecore logging – Create new log file every day

Sitecore have since forever used Log4Net as it’s logging platform. Log4Net is nicely extensible and can be configured to log in many ways.

As per default, Sitecore is configured to start writing to a new log file every time Sitecore starts. And (in some versions of Sitecore) it will continue to write to that file until you restart Sitecore.

You can change that configuration. This configuration will create one and only one log file per day, even if the instance is restarted several times, or never:

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="$(dataFolder)/logs/log" patch:instead="file" />
    <rollingStyle value="Date" patch:instead="rollingStyle" />
    <preserveLogFileNameExtension value="true" patch:instead="preserveLogFileNameExtension" />
    <datePattern value="'.'yyyyMMdd'.txt'" patch:instead="datePattern" />
    <staticLogFileName value="false" patch:instead="staticLogFileName" />
</appender>

Differences from standard Sitecore configuration are:

  • rollingStyle have been changed from Size to Date.
  • The folder name and file date pattern is now split. Property “file” contains the folder and file prefix, and the datePattern property contains the file suffix.
  • staticLogFileName is false, allowing Log4Net to use the datePattern property as file name.

When the configuration is added to a logging.config file, the resulting merge is this:

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" patch:source="logging.config">
    <file value="$(dataFolder)/logs/log" patch:source="logging.config"/>
    <appendToFile value="true" patch:source="logging.config"/>
    <rollingStyle value="Date" patch:source="logging.config"/>
    <maxSizeRollBackups value="-1"/>
    <maximumFileSize value="10MB"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
    </layout>
    <encoding value="utf-8" patch:source="logging.config"/>
    <preserveLogFileNameExtension value="true" patch:source="logging.config"/>
    <staticLogFileName value="false" patch:source="logging.config"/>
    <datePattern value="'.'yyyyMMdd'.txt'" patch:source="logging.config"/>
</appender>

MORE TO READ:

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 6, Sitecore 7, Sitecore 8, Sitecore 9 and tagged , , . Bookmark the permalink.

1 Response to Sitecore logging – Create new log file every day

  1. João Neto says:

    Thanks for sharing. Great idea! Helpful if you want to tail de log file with Notepad ++ or another tool.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.