Using Microsoft.Extensions.Logging with dotnet core console project

2018-01-12

Problem

Next step with my dotnet core console application was to add some logging. I would like to avoid using Console.Write to output state of application as well as well as allow to have some debug/trace level log information.

The way I did it is not the only way but it simples for me, mainly because it reuses already existing extensions which could be used in ASP.NET as well.

Solution

To start, two items are required. First, we need ServiceCollection the DI implementation and the to create empty LoggerFactory.

Second, logging has to be enabled, simply by using extension method for ServiceCollection

The last step is to use configuration. Use any existing configuration for logger factory, for example:

Now the simples way to create object with logger factory is to use dependency injection.


Blog comments powered by Disqus