Sean Holmesby

.NET and Sitecore Developer

By

Real-Time Auto-Reload Logging with Sitecore’s Rolling File Appender

Just found this…you may already know about it, but if not, hopefully it helps you with Sitecore development.

Sometimes you want to tail the log files from Sitecore, and see what’s happening in real-time on your Sitecore site.
But then you do a build or make a config change, logging stops in that file and a new log file is created. You then need to open that new file in the tailing program.

I’ve seen SIM‘s DynamicLogViewer reload the file automatically…. and also Nick Hills’ version of logging using DebugView, but I like Baretail (it has highlighting for easy recognition).

Anyway, I just found you can easily change Sitecore to always log to the one file.

Change:-

<appender name="LogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">

to

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender, Sitecore.Logging">

You can also remove the ‘{date}’ token from the filename…as it’ll all go into the one file.

Now, I just open Baretail on that one file…. watch it, make changes, and it’s still there, even after App pool recycles from builds/config changes etc.

Here are the patches you can use in your local development to do the same.

Pre Sitecore 8.1

For Pre-Sitecore 8.1 versions, log4net exists outside of the node in the Web.config…. so you can just do an XDT transform on it in your Web.Debug.config file, and your build will patch in the new RollingFileAppender values.

<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <log4net>
    <appender name="LogFileAppender" xdt:Locator="Match(name)" xdt:Transform="SetAttributes"
              type="log4net.Appender.RollingFileAppender, Sitecore.Logging">
      <file xdt:Locator="XPath(/configuration/log4net/appender[@name='LogFileAppender']/file)" xdt:Transform="SetAttributes"
            value="$(dataFolder)/logs/log.txt" />
    </appender>
    <appender name="WebDAVLogFileAppender" xdt:Locator="Match(name)" xdt:Transform="SetAttributes"
              type="log4net.Appender.RollingFileAppender, Sitecore.Logging">
      <file xdt:Locator="XPath(/configuration/log4net/appender[@name='WebDAVLogFileAppender']/file)" xdt:Transform="SetAttributes"
            value="$(dataFolder)/logs/WebDAV.log.txt" />
    </appender>
    <appender name="SearchLogFileAppender" xdt:Locator="Match(name)" xdt:Transform="SetAttributes"
              type="log4net.Appender.RollingFileAppender, Sitecore.Logging">
      <file xdt:Locator="XPath(/configuration/log4net/appender[@name='SearchLogFileAppender']/file)" xdt:Transform="SetAttributes"
            value="$(dataFolder)/logs/Search.log.txt" />
    </appender>
    <appender name="CrawlingLogFileAppender" xdt:Locator="Match(name)" xdt:Transform="SetAttributes"
              type="log4net.Appender.RollingFileAppender, Sitecore.Logging">
      <file xdt:Locator="XPath(/configuration/log4net/appender[@name='CrawlingLogFileAppender']/file)" xdt:Transform="SetAttributes"
            value="$(dataFolder)/logs/Crawling.log.txt" />
    </appender>
  </log4net>
</configuration>

Sitecore 8.1+

For 8.1, the Web.config was overhauled, and the majority of it was moved into App_Config/Include/Sitecore.config. The log4net section was also moved inside the node, so we can just patch it with Sitecore’s patching system now instead of the XDT transform. You can drop a new patch file inside App_Config/Include with the following:-

 
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <log4net>
        <appender name="LogFileAppender">
            <patch:attribute name="type">log4net.Appender.RollingFileAppender, Sitecore.Logging</patch:attribute>
            <file>
                <patch:attribute name="value">$(dataFolder)/logs/log.txt</patch:attribute>
            </file>
        </appender>
        <appender name="WebDAVLogFileAppender">
            <patch:attribute name="type">log4net.Appender.RollingFileAppender, Sitecore.Logging</patch:attribute>
            <file>
                <patch:attribute name="value">$(dataFolder)/logs/WebDAV.log.txt</patch:attribute>
            </file>
        </appender>
        <appender name="PublishingLogFileAppender">
            <patch:attribute name="type">log4net.Appender.RollingFileAppender, Sitecore.Logging</patch:attribute>
            <file>
                <patch:attribute name="value">$(dataFolder)/logs/Publishing.log.txt</patch:attribute>
            </file>
        </appender>
    </log4net>
</sitecore>

Here is my patch config file for the Rolling Logger in Sitecore 8.1. Place it in a App_Config/Inlucde/zzz folder so you can be sure it’ll patch the site correctly.
https://gist.github.com/SaintSkeeta/f3aa74e7d17875bdb39a3f87515ebc07

3 Responses to Real-Time Auto-Reload Logging with Sitecore’s Rolling File Appender

  1. I didn’t want to make any config changes and still use Baretail. You can use this batch script to automatically open the latest log file. Obviously, you will have to restart after your server restarts. But I don’t mind that.

    @echo off
    set logFolderPath=[path to your logs folder with ending backslash]
    for /f “tokens=*” %%a in (‘dir %logFolderPath%log.*.txt /b /od’) do set newest=%%a
    start baretail.exe “%logFolderPath%%newest%”

  2. Jose says:

    Hey Sean, have you experienced any issues using rolling appender? We found out it would not generate any log files occasionally when using this.

    • sholmesby says:

      Yeah, this is because the worker process still holds onto the application a little long, so the log is tied to the old worker process, and there are issues releasing it. Doing a second app pool recycle seems to bring it back…. which is a bit annoying….

Leave a Reply

Your email address will not be published. Required fields are marked *