-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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.
Install the NLog.Extensions.Logging NuGet package:
dotnet add package NLog.Extensions.Logging
Create a LoggerFactory and include AddNLog():
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using var loggerFactory = LoggerFactory.Create(builder =>
{
// Remove default Microsoft logging providers
builder.ClearProviders();
// Register NLog
builder.AddNLog();
});
var logger = 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.
NLog supports several configuration approaches:
- NLog.config - Classic NLog configuration style.
-
appsettings.json -
AddNLog()automatically loads the"NLog"section if present, with environment overrides. - Fluent Configuration API - Configure NLog directly using C#.
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:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwConfigExceptions="true"
autoReload="true">
<targets async="true">
<target xsi:type="Console" name="console" layout="${MicrosoftConsoleLayout}" />
<target xsi:type="File" name="file" fileName="logs/app-${shortdate}.log" maxArchiveFiles="7">
<layout xsi:type="MicrosoftConsoleJsonLayout" includeScopes="true" includeActivityIds="true" />
</target>
</targets>
<rules>
<!-- Reduce noise from .NET Core Host, and route relevant logging to output targets -->
<logger name="System.*" finalMinLevel="Warning" />
<logger name="Microsoft.*" finalMinLevel="Warning" />
<logger name="Microsoft.Hosting.Lifetime*" finalMinLevel="Info" />
<logger name="*" minLevel="Info" writeTo="console,file" />
</rules>
</nlog>This configuration includes:
- Targets — Define output destinations for writing log messages. See NLog Targets
- Rules — Define how log messages are routed from loggers to output targets.
- Layout — Define how log messages are formatted. See NLog Layouts
- Layout Renderers - Provide additional details to include in log output. See NLog Layout Renderers
The Console-target ensures that .NET Core hosting lifetime startup messages are available on the console. The file-target writes newline-delimited JSON log events that capture structured logging properties and trace context (TraceId and SpanId) for correlating logs with traces in distributed systems.
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.
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.
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.
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json