Orchard Core Introduction - Part 3 : Adding Logging to Orchard Core

One of the key things required for development and troubleshooting a large CMS implementation is quality error logging and information tracking. Legacy Orchard leveraged log4net as it's method to log system errors and information. This was very helpful and allowed good insight into each module and what was happening all the way from the front to the back of the system. In Orchard Core, log4net has been replaced with NLog. Why they changed? I don't know, but perhaps NLog is a more logical system for .NET Core. 

Getting logging configured requires a few steps to complete it, and then you should be off and running! I have to give a shout-out to the team at IDeliverable for their early blogging on the Orchard Core logging setup. Their tutorial helped me out a lot, but I noticed in recent builds the steps to set this up have been somewhat refactored. So the steps below are how you should now perform the process.

Step 1 - Download OrchardCore.Logging.NLog

As the naming convention indicates, there probably will be the possibility to extend Orchard.Logging with your own logging framework or log4net if you wish. This makes Orchard Core very plug-and-play. Browse to your Nuget Package Manager and download the Nuget Package OrchardCore.Logging.NLog. Remember, your package source in the top left should be set to the Orchard Core gallery to find this package. The steps to setup this Nuget source were outlined in Part 1 of this series.

Step 2- Download the Nlog.config file from Orchard Core Github

In order for Nlog to log properly, you need a configuration file. You can dowload the base configuration file from the Orchard Team here : https://github.com/OrchardCMS/OrchardCore/tree/dev/src/OrchardCore.Cms.Web

Place this file in the root of your Orchard Core Web application and include it in your project.

Step 3 - Update your Program.cs file to Use NLog

The final step is to update your Orchard Core Web application's Program.cs file to add NLog to the pipeline. It's a one-line where you add .UseNLogWeb() to your WebHost builder. You will also need to add a reference to OrchardCore.Logging via a using statement as well.

That's it! You should now see a logs directory in your App_Data folder with a new logfile. 

Step 4 - Add some more .NET Core logging via the appsettings.json file.

One thing I noticed recently as I have been learning .NET Core is that there is a new appsettings.json file. This file controls the trace level for your application.

You should grab the one here at https://github.com/OrchardCMS/OrchardCore/blob/dev/src/OrchardCore.Cms.Web/appsettings.json and put it in your webroot.

Questions for the Cloud team? Please browse to our Contact Us page and let us know how we can help you get started with Orchard!