In this tutorial, you will learn how to write custom log messages within Sitecore CMS. Sitecore CMS uses Log4Net as its logging provider. By extending Log4Net and using its APIs, you can write custom log messages to the log files created by Sitecore. Log4Net will allow you to write different types of logs, including errors, debug, information and warning. If you want to learn how to do this within your website, read on πŸ”₯πŸ”₯πŸ”₯

If you look in your websites weboort, you should see a folder called logs. After running your site for a while, you should see a jumble of Sitecore log files start to appear here:

How To Write Log4Net Logs Into A Different File In Sitecore 1

This is the folder location that you will need to visit when trying to debug a production or development issue. If you have a lot of traffic within production, it can often be really hard to find the correct log files for the issue. This is why a lot of people like to start writing their own custom log messages into a separate custom log file that is bespoke to their application. With Log4Net this is possible and fairly easy 😎

On most projects, I will always favour creating custom separate log files. First, it's very quick and easy to do. Second, 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.

Log4Net

If you look in the bin folder within your website's webroot, you should see an assembly called Sitecore.Logging.dll. Sitecore.Logging.dll contains Sitecores custom wrapper around the Log4Net provider. If you open your web.config file and search, you can see that the out-of-the-box default Sitecore logger will be set to use it:

If you want to write logs to your own custom log file, we will need to do some tinkering within this section πŸ€”

Custom Appender

To create a custom log file, you will need to create a custom appender. You create a custom appended in config. In this example, my appender will look like this:

Note that the appender is using the Sitecore specific logger, log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging instead of the default Log4Net one. For reference, the default .NET Log4Net appender would be called log4net.Appender.FileAppender. In the file section, we use the $(dataFolder) to specify where on the server you want the log files to be created. I recommend you pick your Sitecore instances data folder. In this example, my logs will be written to a folder within Data called Custom.

The layout section is also important. This defines the format of the log files when Log4Net writes them. Sitecore has defined some shortcuts in this area that you can use:

  • %d: Date
  • %n: New Line
  • %m: The message
  • %4t: Thread Id
  • %-5p: Logging Level

There are a few more options you can use. If you want to be very specific about the format and you can not see something in the list, Google is your friend 😊. You have now configured Log4Net with an appender called CustomLogFileAppender. The next thing we need to do is define a Logger so we can use it in code. This split between Logger and Appender is useful. It means you can create multiple loggers that all write to the same log file. You can then set each logger to write data using different levels of information so this gives you a lot of power. Personally, I've never needed to get this granular, in my logging. The Logger definition should look similar to this:

In terms of configuration, we are done. The next step is to create a Logger instance in your codebase and log something to it. To define a logger you can use this code (NOTE: the Logger names must match):

Next, we need to call the logger with something to log:

After writing a log, check the log files. Assuming you copied the same config as me, if you look here:

Data ➑ Logs ➑ CustomLogs

We should now see some log files:

How To Write Log4Net Logs Into A Different File In Sitecore 2

In the log files we can now see some data:

You now have a mechanism to write custom logs in SitecoreπŸ’₯


You now know how to log errors to a disk using Log4Net and Sitecore. It is easy to customise the Log custom data into a log file of our choosing. Happy Coding 🀘