Umbraco 8 comes with a new logging framework, serilog. In this tutorial, you will learn all about Serilog, how to access the logs, and how to write your custom messages to the logs. Historically, Umbraco V7 and earlier used Log4Net. Log4Net is a great logger but it is very limited in terms of searching the logs. Everything was stored within a .test file and you would need to manually try to read and parse the data yourself.

Out of the box V8 now writes to a JSON log file meaning v* has more feature-rich tools to perform searches & correlation on log patterns a lot easier. The default log file location can be found within 'App_Data/Logs' and contains the Machine name, along with the date too:

Using a JSON object to store log data instead of within a flat-file, means querying that data is a lot easier, for example:

Gives a lot more power compared to search compared to a log within a te file, like this:

This new logging also comes with a dashboard within the CMS that will allow you to query the logs with a lot more power.

How To Hook Into The Umbraco Logs

As you would expect you can also write to the Umbraco Logs. This can be done like this:

How you log to Serilog is important as it can affect what data is generated within the log. Avoid using string interpolation or string concatenation when you write to the log as it will not get a separate property logged with the message.

Take these different ways to write to the log file:

All these approaches are essentially doing the same thing. Writing a parameter to the log files. All will work, however, the good practice techniques will give you better logging. Let us take a deeper dive.

One bad practice technique uses string interpolation and the other uses string concatenation to append the extra data to the log file. This results in these two log files being generated:

And

Compare this with a good practice example:

When you compare the results for doing the same thing you can see the good practice actually logs a lot more information than the other two. Following the good practice technique, you could search for 'Good Practice {example}' and find all the related logs. You will also have all the parameters logged separately with the name in the correct format rather than a helpful '@1'.

Another useful thing to note is that all the major Umbraco base classes you can use all have access to the Logger. If you are using either RenderMvcController, SurfaceController, UmbracoApiController, or, UmbracoAuthorizedApiControlleryou can use the base 'Logger' property without the need to explicitly type it, like this:

Like most other V8 things you can inject the logger using DI for everything else:

Enjoy!