You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Getting started with .NET Core Console application
NLog integrates with the built-in Microsoft.Extensions.Logging (MEL) infrastructure. Application code continues to use ILogger<T>, while NLog handles log routing, formatting, and writing to configured targets.
This guide shows how to configure NLog as a logging provider for a .NET console application.
usingMicrosoft.Extensions.Logging;usingNLog.Extensions.Logging;usingvarloggerFactory=LoggerFactory.Create(builder =>{// Remove default Microsoft logging providersbuilder.ClearProviders();// Register NLogbuilder.AddNLog();});varlogger=loggerFactory.CreateLogger<Program>();logger.LogInformation("Application started.");
AddNLog() registers NLog as the logging provider for the Microsoft.Extensions.Logging LoggerFactory. From this point log messages will be processed by NLog.
This guide uses an NLog.config XML file as the configuration example, but the alternatives above also work. Create a file named NLog.config in the application project root directory and add the following content:
Ensure that the NLog.config file is copied to the output directory when building or publishing the application. In Visual Studio, set the Copy to Output Directory property to Copy if newer.
4. Start logging
NLog is now configured in the application. Start the application, and existing ILogger<T> calls will be processed by NLog and written to the configured targets.
You are not required to use the ILogger<T>, if you prefer using NLog.LogManager.GetCurrentClassLogger() in the console application.
If NLog is not working as expected, see the NLog Logging troubleshooting guide to identify configuration issues.
Using the .NET Generic Host
If the console application uses the .NET Generic Host, NLog can be registered directly with the host using UseNLog().
See the NLog.Extensions.Hosting NuGet package for information about integrating NLog with the .NET Generic Host.