In this tutorial, you will learn how to set up file-based logging within an Optimizely CMS 12 powered website. You may be thinking of logging, big-deal, however, life is not as straightforward as it used to be. In the old days, you used to install the CMS and out-of-the-box it came with Log4Net, job done πŸ’₯. Life is not as straightforward with ASP.NET 5. An ASP.NET 5 powered website, can be hosted on a range of different platforms, Linux, Windows, Mac. This means the process of logging is not a simple single-path solution. This flexibility has some trade-offs.

Optimizely CMS now uses the default ASP.NET 5 logging provider. The benefit of this switch is that you have a lot more flexibility due to third-party extensions. The downside is that you will need to invest more initial start-up effort. ASP.NET 5 does not and will not support file-based logging. I personally find file-based logging really useful in development and production. When you install the CMS the logger will only log things to the console which is not ideal. This is why learning what logging options you have available to you and how to configure the provider is key to being an OPtimizely super-star. If you want to learn to be a start, read on πŸ”₯πŸ”₯πŸ”₯

Logging 101

First, let us start with how to write custom logs. In CMS 12 there are two ways to write custom logs. You can use ILogger<T>:

ILogger<T> is part of the Microsoft.Extensions.Logging.Abstractions package. AS you can tell from the signature ILogger uses generics, where T is the name of the calling class. Using this logger you can different types of logs. Depending on your logging needs you can set an error to be Trace, Debug, Error, or Critical. The alternative way to write custom logs using the Episerver LogManager. As the documentation for LogManager points out it is simply a façade around Microsoft.Extensions.Logging. As the LogManager does not use dependency injection, I do not recommend you use it within your projects, however, if you are really keen, here's the code:

Regardless of which API you use, how and where the logger works are defined within appsettings.json:

The default LogLevel is set to Information. If you find this is logging too much crap in your logs, change this to Warning.

File-Based Logging

If you ran the code above, it will log messages to the console. This is obviously not ideal for a production, or, development environment. In development, I prefer logs to be written to disk so that I can view the errors when I damn well feel like πŸ’ͺπŸ’ͺπŸ’ͺ. The overarching point is that when you set up the CMS, you will likely need more logging capabilities than simply writing to the console. This is why on most projects, you will need to set up a third-party Logging Provider on most projects.

There are a number of third-party logging providers that you can choose from. Depending on your needs will influence which provider you will need to use. If you simply want to write log files to disk, the logging provider I recommend you checkout is Serilog. I have been using Serilog with another CMS for several years and it works really well. The feature I like with Serilog is the ability to write logs to disk and it has the ability to write those logs as JSON objects. Writing logs to JSON is handy as it will allow you to query the logs in ways that are not possible with txt logs. For this tutorial, I will assume you agree with Serilog. In order to get Serilog working with JSON, you will need to install these packages:

You can configure Serilog in one of two ways, at the code level or the config level. You can configure the logging provider within Program.cs using this code:

The limit of configuring the provider at the code level is that it will require a code change to run. It will also work the same way in all environments. A more flexible way to configure the logger is via appsettings.json. To use configuration from appsettings.json' you would adjustProgram.cs` like this:

The nice thing about this approach of using a property means ConfigureAppConfiguration and LoggerConfiguration can share the same config. Handy!

To configure the provider you need to add some JSON into app/settings:

This configure will write the logs to three locations. The console, a text file on disk within a folder called Log in the webroot, and a JSON file in the same Log folder. Obviously, you do not need all three, I have left it and you can decide which path you would like to take.

Log Viewer

To make life easier when viewing your logs, you can use a third-party logging tool. There are a few on the market, some paid some free. An open-source one you can try out for free is TailBlazer.


The benefit of being able to host your website on any environment does come with some penalties. When building ASP.NET 5 CMS websites you will need to spend extra time setting up things that just worked in ASP.NET Framework. Logging is one of those items. The good news is that after you armed with the correct knowledge setting everything up is pretty straightforward. Happy Coding 🀘