Sitecore Logging

Sitecore Logging

One of the advantages of using a platform like Sitecore over completely bespoke development, is the number of features that are built-in that day to day you completely take for granted. An important one of those is logging.

If you're building a bespoke application, adding some sort of support for generating log files can be a bit of a pain. Granted there are solutions that can be added to your project that do most of the lifting for you, but you still need to think about it, decide which to use and understand how to use it. With Sitecore the ability to write to a log file has been built in along with the logic that's going to delete old log files and stop your servers hard disk filling up. Under the hood Sitecore is using Log4Net to generate log files, a side effect of this is that config changes can not be made using patch files.

Logs are written to the logs folder within your data folder. If your on Sitecore 8.2 or below this will be adjacent to your website folder. If your on Sitecore 9 or using Sitecore PaaS this will be in the App_Data folder within your sites folder.

The different log files

Sitecore generates 6 different log files while it's running, these are:

Log - A general log file which you can write to
Crawling - Logs from the Sitecore Search Providers for crawling
Search - Logs from the Sitecore search providers for searches
Publishing - Logs generated during Sitecore publishes
FXM - Logs from federated experience manager
WebDAV - A log for WebDAV activity

Customizing the amount of detail

At different times you will likely want to see a different amount of detail in your log files. For instance on a production server you will want to keep logs to a minimum to maximise performance. However in a different environment where you are trying to debug an issue you would want all the logs you can get.

For this reason when writing a log a priority level is assigned and each log file can be configured to only write logs at a certain level or below to disk.

Priority levels are:

  1. DEBUG
  2. INFO
  3. WARN
  4. ERROR
  5. FATAL

To configure what level of logging should be output, configure the priority value in the log4net section of the web.config file.

1<log4net>
2 <appender name="LogFileAppender"
3 type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
4 <file value="$(dataFolder)/logs/log.{date}.txt" />
5 <appendToFile value="true" />
6 <layout type="log4net.Layout.PatternLayout">
7 <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
8 </layout>
9 </appender>
10 <root>
11 <priority value="INFO" />
12 <appender-ref ref="LogFileAppender" />
13 </root>
14</log4net>

Writing to the log file

Writing to the log file is super easy to do from within your Sitecore application. The Sitecore.Diagnostics.Log class contains static functions to write to the general log file at different priority levels.

1// Writes a log at the error priority level
2Sitecore.Diagnostics.Log.Error("That wasn't meant to happen", this);