ca9163d9
ca9163d9

Reputation: 29159

Why doesn't NuGet package NLog work for .NET Core?

The following steps are done.

  1. Create a new .NET Core console project (C# .NET Core 2.1)

  2. Add the NuGet package NLog, NLog.Config (currently it's V4.5.8)

  3. Add the logger field in class Program.

     static Logger logger = LogManager.GetCurrentClassLogger();
    
  4. Add logger.Info("Test NLog"); in static void Main(string[] args).

However, why will running the program not print "Test NLog"?

Upvotes: 4

Views: 7778

Answers (3)

Rolf Kristensen
Rolf Kristensen

Reputation: 19847

Ensure not to use NLog.config NuGet package. It is deprecated and should not be used for new project (it is also said in the description of the NuGet package).

It has the unwanted side-effect of resetting the actual Nlog.config on application-deployment. The NuGet package became broken when Microsoft decided to redesign the NuGet package format.

See also: Getting started with .NET Core 2 Console application

Upvotes: 2

Swoogan
Swoogan

Reputation: 5548

The NLog.Config package does not add a configuration file to your project, and you will have to create one manually. You will also have to manually ensure that it is copied to the output directory.

A simple configuration file is as follows:

<?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">

    <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
        <target name="logconsole" xsi:type="Console" />
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="logconsole" />
        <logger name="*" minlevel="Debug" writeTo="logfile" />
    </rules>
</nlog>

Make sure that the following is added to your .csproj file:

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

Make sure the type is None and not Content as is shown for ASP.NET projects. Console applications do not copy Content files.

Also, be sure that the first line of the configuration file is the <?xml ... element. It's an obscure issue but if it's not on the first line NLog will not be able to parse the file.

When all else fails, refer to the excellent troubleshooting guide: https://github.com/NLog/NLog/wiki/Logging-troubleshooting

Upvotes: 0

Julian
Julian

Reputation: 36710

NLog fully supports .NET Core 1 and 2 since NLog 4.5

The issue is probably an issue with nuget, that the nlog.config isn't installed with the NLog.Config package.

You should check your nlog.config (and also deploy it). If it isn't there, then create it.

I would recommend to read:

However, running the program will not print "Test NLog"?

Where do you expect the output? Check your nlog.config also for this

Upvotes: 1

Related Questions