Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 671 Bytes

README.md

File metadata and controls

29 lines (22 loc) · 671 Bytes

How to use NLog in a .Net Core Console App

Setup DI

var services = new ServiceCollection();

//add logging
services.AddLogging();

//build the provider;
var serviceProvider = services.BuildServiceProvider();

Source: AndrewLock.Net

Configure NLog

var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
loggerFactory.AddNLog();
loggerFactory.ConfigureNLog("nlog.config");

Create a Logger and Log Something

logger = serviceProvider.GetService<ILoggerFactory>()
    .CreateLogger<Program>();

logger.LogInformation("Hello World!");