How To Write Log4Net Logs Into A Different File In Sitecore

Like a lot of other CMS systems, Sitecore uses Log4Net to write errors, debug information and warning to disk. If you look in your website data folder, you should see a folder called 'logs'. If you look in here you should see a jumble of Sitecore log files: sitecore_logs In a production system, if you have mission critical functionality, you will more than likely want to have specific files for these key areas. If your enquiry form went down, for example, which generated all of your company's leads, then you need to get the functionality back to normal, as quickly as possible. In most CMS'es that use Log4Net, I will always favour creating seperate log files, as a) it's very quick and easy to do, and b) it makes life so much easier to debug your custom code when you don't have to shift through all the CMS logs and warnings.


If you look in the 'bin' of your website, you should see an assembly called 'Sitecore.Logging.dll'. This is Sitecore's own custom variant of the Log4Net provider. If you open your web.config file and search,  you can see that the out of the box Sitecore logger, uses it:
    <appender name="LogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
      <file value="$(dataFolder)/logs/log.{date}.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
      <encoding value="utf-8" />
If you want to create your own custom log file, we will need to do some tinkering within this section.

Custom Appender

As the above snippet suggests, we will need to create a custom appender. Mine will look like this:
    <appender name="CustomLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
      <file value="$(dataFolder)/Logs/Custom/CustomLog{date}.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="%d (%-5p) %n%m%n%n" />
So the things to note in here are: The type uses the Sitecore specific logger, 'log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging', instead of the default log4net ones, like 'log4net.Appender.FileAppender'. In the file section, we use the $(dataFolder) to specify our Sitecore instances data folder. In my example, I'm adding mine to a new folder call 'Custom'. The layout section is also important. This defines how your log file will look. Sitecore have defined some shortcuts in this area:
  • %d = Date
  • %n = New Line
  • %m = The message
  • %4t = Thread Id
  • %-5p = Logging Level
The rest are bog standard log4net stuff and should, hopefully, be self-explanatory. You can Google the rest if you really feel the urge. Now we have told Log4Net how we want to Log this appender called 'CustomLogFileAppender', the next thing we need to do is define it as a 'Logger' so we can use it in code. This split between Logger and Appender means you can have different Loggers, log different levels of information into the same file. Personally, I've never needed to get this granular, but, it won't work unless it gets set. The Logger definition will still live in the section.
    <logger name="CustomLogger" additivity="false">
      <level value="ERROR"/>
      <appender-ref ref="CustomLogFileAppender"/>
In terms of configuration, we are done. The next step is to create a Logger in a class somewhere and then log something with it. To define a logger you can use this code:
        private static readonly ILog Logger = LogManager.GetLogger("CustomLogger");
NOTE Note the Logger names must match Next, we need to call the logger with something to log:
                // Code
            catch (Exception ex)
                Logger.Error("Log An Error", ex);
If we look in 'Data' -> 'Logs' > 'CustomLogs' we should now see some log files: sitecore_logs_2 IN the log files we can now see some data:
14:01:15 ERROR  Error

14:01:15 INFO  Info


In today's guide, we've talked a little about how Sitecore logs errors to disk (Log4Net) and how we can easily customise it to Log custom data into a log file of our choosing.

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top