Why is logging so important during the application development? Well, while your application is in the developing stage it is very easy to debug the code and to find out what went wrong. But, can you debug in the production environment?

Of course not. 

That is why logging messages are a great way to find out what went wrong and where did error happen in your code in the production environment. .NET Core has its own implementation of logging messages, but in all my projects, I prefer to create my own custom logger service. 

This is what I am going to show you in this post.

If you want to see all the basic instructions and complete navigation for this series, please follow the following link: Introduction page for this tutorial.

For the previous part check out: Creating .NET Core WebApi project – Basic code preparations

Source code to download is available at this link: .NET Core, Angular 4 and MySQL. Part 3 – Source Code

This post is divided into several sections:

Creating Required Projects

Let’s create two new projects. Name the first one Contracts. You are going to store the interfaces inside this project. Name the second one LoggerService. You are going to use it for the logger logic.

To create a new project, right click on the solution window, choose Add and then NewProject. Under the .NET Core, choose the Class Library (.NET Core) and name it Contracts.

create-new-project-contracts .net core logging

Do the same thing for the second project, just name it LoggerService.

With these two projects in place, we need to reference the LoggerService project to the main project. In the main project inside solution explorer, right click on the Dependences and choose the AddReference. Under the Projects click the Solution and check this project:


Before we proceed to the implementation of the LoggerService, let’s do one more thing.

In the LoggerService project right click on the Dependencies and then click on the Add Reference. Inside check the Contracts checkbox to import its reference inside the LoggerService project. This would automatically add the Contracts reference to the main project because we already have a LoggerService referenced inside.

Creating the Interface and Installing NLog

Our logger service will contain four methods for logging:

  • Info messages
  • Debug messages
  • Warning messages
  • And error messages

Consequently, you will create the interface ILoggerManager inside the Contracts project containing those four method definitions.

Add the following code to the ILoggerManager interface:

Before you implement this interface inside the LoggerService project, you need to install the NLog library in our LoggerService project. NLog is logging platform for the .NET which will help us create and log our massages.

I’ll show you two ways how to add NLog library to your project.

  1. In the LoggerService project, right click on the Dependences and choose Manage NuGet Packages. After the NuGet window appears, just follow the steps:

NLog-nuget .net core logging

  1. From the View menu choose the Other Windows and then click on the Package Manager Console. After the console appears just follow the leads:

package manager .net core logging

After a couple of seconds, NLog is up and running in your app.

Implementing Interface and Nlog.Config File

In the LoggerService project create the new class LoggerManager.

Let’s modify the LoggerManager class:

Now, we need to configure it and inject it into the Startup class in the ConfigureServices method.

NLog needs to have information about the folder to create log files in, what the name of these files will be and what a minimum level of logging is. Therefore, you need to create one text file in the main project with the name nlog.config and to populate it as in the example below. You need to change the path of the internal log and filename parameters to your paths.

Configuring Logger Service for Logging Messages

Setting up the configuration for a logger service is quite easy.

First, you must update the constructor in the Startup class:

ILoggerFactory has its implementation inside .NET Core and it serves the purpose of configuring the logging system inside it. With the extension method ConfigureNLog, we are forcing the ILoggerFactory to apply the configuration of our new logger from the XML file.

If you have installed a newer version of NLog (in this article we are working with 1.0.0-rtm.rc3 version) then this implementation inside the Startup class is obsolete. Now you should do your registration in this way:

Secondly, you need to add the logger service inside the .NET Core’s IOC container. There are three ways to do that:

  • By calling services.AddSingleton which will create the service the first time you request it and then every subsequent request is calling the same instance of the service. This means that all components are sharing the same service every time they need it. You are using the same instance always
  • By calling services.AddScoped which will create the service once per request. That means whenever we send the HTTP request towards the application, a new instance of the service is created
  • By calling services.AddTransient which will create the service each time application request it. This means that if during one request towards our application, multiple components need the service, this service will be created again for every single component which needs it

So, in the ServiceExtensions class add a new method:

Lastly, in the ConfigureServices method invoke this extension method, right above services.AddMvc():

Every time you want to use a logger service, all you need to do is to inject it into the constructor of the class that is going to use that service. .NET Core will serve you that service from the IOC container and all of its features will be available to use. This type of injecting objects is called Dependency Injection.

DI, IOC and Logger Service Testing

What is exactly Dependency Injection(DI) and what is IOC (Inversion of Control)?

Dependency injection is a technique for achieving loose coupling between objects and their dependencies. It means that rather than instantiating an object every time it is needed, we can instantiate it once and then serve it in a class, most often, through a constructor method. This specific approach we utilize is also known as the Constructor Injection.

In a system that is designed to use DI, you may find many classes requesting their dependencies via their constructor. In that case, it is helpful to have a class which will provide all instances to classes through the constructor. These classes are referred to as containers or more specific Inversion of Control containers. An IOC container is essentially a factory that is responsible for providing instances of types that are requested from it.

For the logger service testing purpose, you could use ValuesController. You will find it in the main project in the Controllers folder. 

In the Solution Explorer open the Controllers folder and open the ValuesController class. Let’s modify it:

After all, start the application, and browse to http://localhost:5000/api/values. As a result, you will see an array of two strings. Now go to the folder that you have specified in the nlog.config file, and check out the result. You should see four lines logged inside that file.

logged file .net core logging


Now you have the working logger service, you will use through an entire process of our application development.

By reading this post you’ve learned:

  • Why logging is important
  • How to create external service
  • How to use NLog library and how to configure it
  • What are DI and IOC
  • And how to use them for service injection

Thank you for reading and see you soon in the next blog post where we will talk about repository pattern with entity framework core.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.