FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Getting started with .NET Core 2 Console application · NLog/NLog Wiki · GitHub

/ NLog Public

Getting started with .NET Core 2 Console application

Rolf Kristensen edited this page Aug 8, 2026 · 39 revisions

Description

Explains how to setup NLog as logging provider for .NET Core and Microsoft Extension Logging (MEL).

Notice that NLog can be used on .NET Core without help from Microsoft Extension Logging and NLog.Extension.Hosting. See standard NLog Tutorial.

0. Create a new .NET Core console project

In Visual Studio 2017, using .NET 4.6.1+ or .NET Core 2

1. Add dependency in csproj manually or using NuGet

Install:

e.g.

  <ItemGroup>
    <PackageReference Include="NLog" Version="5.4.0" />
    <PackageReference Include="NLog.Extensions.Logging" Version="5.4.0" />
  </ItemGroup>

2. Create a nlog.config file.

Create nlog.config (lowercase all) file in the root of your application project (File Properties: Copy Always)

We use this example:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      internalLogFile="c:\temp\console-example-internal.log"
      internalLogLevel="Info" >

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="logfile" fileName="c:\temp\console-example.log"
            layout="${longdate}|${level}|${message} |${all-event-properties} ${exception:format=tostring}" />
    <target xsi:type="Console" name="logconsole"
            layout="${longdate}|${level}|${message} |${all-event-properties} ${exception:format=tostring}" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="*" minlevel="Trace" writeTo="logfile,logconsole" />
  </rules>
</nlog>

For an introduction to the NLog.config-file then see the NLog Tutorial, and for more detailed information then see here.

Ensure to configure your project-file to copy NLog.config to the output directory:

 <ItemGroup>
     <None Update="nlog.config" CopyToOutputDirectory="Always" />
 </ItemGroup>

3. Update your program

3.1 Create your runner class

 public class Runner
 {
     private readonly ILogger<Runner> _logger;

     public Runner(ILogger<Runner> logger)
     {
         _logger = logger;
     }

     public void DoAction(string name)
     {
         _logger.LogDebug(20, "Doing hard work! {Action}", name);
     }
 }

3.2 Setup Microsoft Logging with Dependency Injection

using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;

static void Main(string[] args)
{
   // Disposing the ServiceProvider will also flush / dispose / shutdown the NLog Logging Provider
   using var servicesProvider = new ServiceCollection()
      .AddTransient<Runner>() // Runner is the custom class
      .AddLogging(loggingBuilder =>
      {
         // Setup NLog for logging
         loggingBuilder.ClearProviders();
         loggingBuilder.AddNLog();
      }).BuildServiceProvider();

   var runner = servicesProvider.GetRequiredService<Runner>();
   runner.DoAction("Action1");

   Console.WriteLine("Press ANY key to exit");
   Console.ReadKey();
}

4 Example output

On screen:

In file:

2017/10/16 23:08:46.479|DEBUG|Doing hard work! Action1 |ConsoleExample.Runner|Action=Action1, EventId_Id=20, EventId_Name=, EventId=20

ℹ️ See also example in GitHub

Wiki pages Pages 295

Clone this wiki locally


Back | FazBrowse Home | New Git URL