One of the key aspects required for troubleshooting an application is creating a quality logging system. We all know the need for logging the code we create. Logging is essential to understanding the behaviour of an application and to debug unexpected issues or for simply tracking events. In the production environment, we can’t debug issues without proper log files as they become the only source of information to debug some intermittent or unexpected errors.
Launch Visual Studio and load the MyOrchardCoreCMS solution. You can get the source here. Right click on "Manage NuGet Packages..." in the project dependencies.
Select the "Browse" tab. In the search box search for "OrchardCore.Logging.NLog", make sure the "Include prerelease" is checked, and press the "Install" button.
Press the "OK" button to accept changes.
Add a new Item to the project.
Select a "Test File".
Rename it to "NLog.config".
Add the following to the config file:
<?xml version="1.0" encoding="utf-8" ?>
Level Typical Use
Fatal Something bad happened; application is going down
Error Something failed; application may or may not continue
Warn Something unexpected; application will continue
Info Normal behavior like mail sent; user updated profile etc.
Debug For debugging; execute query; user authenticated, session expired
Trace For trace debugging; begin method X, end method X
<!-- file target -->
<target xsi:type="File" name="file"
<!-- console target -->
<target xsi:type="Console" name="console" />
<!-- all warnings and above go to the file target -->
<logger name="*" minlevel="Warn" writeTo="file" />
<!-- the hosting lifetime events go to the console -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="console" />
Modify the "Programs.cs" file to:
public class Program
public static Task Main(string args)
public static IHost BuildHost(string args) =>
.ConfigureLogging(logging => logging.ClearProviders())
.ConfigureWebHostDefaults(webBuilder => webBuilder
Modify the "appsettings.json" file to:
Here are the logging levels described.
Here is the location of the log file.
We modified the MyOrchardCoreCMS Web Application, created on a previous video and added Orchard Core logging. We went through the coding and configuration settings, Went over all the the different logging levels (Fatal, Error, Warn, Info, Debug and Trace). Then we ran the application, After exiting the application. We opened the detailied log and followed it's logging events. Logging in any application is very important, you can log a lot of critical things and gain unprecedented insight into how your application is working and performing. It is a language/framework agnostic practice which I recommend you to start from today if you are not already doing it.
The complete source code is located here.