.NET Core Class Library with NLog targeting .Net 4.6.1

1k views Asked by At

I'm currently creating a common library from my work that would implement an abstracted logger using NLog.

I've created a .NET Core class library targeting .NET 4.6.1 and implemented NLog, but when I try to execute a unit test that I've created, I've noticed that the nlog.config file is not being picked up. I've also followed the instruction stated https://github.com/NLog/NLog.Extensions.Logging, but I've noticed that it only targets Asp.Net Core and not for .NET Core Class Library solution.

My question is, (given that it is my first time to foray to .NET core) is the instructions stated on the link above applicable for .Net Core class libraries too, or is there a different way to implement this?

Or should I end up not using .Net Core and go for more traditional .NET Class library implementation instead?

Many thanks!

1

There are 1 answers

1
Julian On BEST ANSWER

I've created a .NET Core class library targeting .NET 4.6.1 and implemented NLog, but when I try to execute a unit test that I've created, I've noticed that the nlog.config file

It difficult to find the config file when running as unit test. Different frameworks are using different locations. The following options should work

  1. Manually find the config file location as pass it to NLog. Use

    LogManager.Configuration = new XmlLoggingConfiguration(filePath, true);
    

    instead of ConfigureNLog() , or

  2. Create a config with the API, or
  3. Create the XML as string and feed it to NLog: instead of ConfigureNLog, use

    XElement element = XElement.Parse(configXml);
    LogManager.Configuration = new XmlLoggingConfiguration(element.CreateReader(), "nlog.config");
    

I've noticed that it only targets Asp.Net Core and not for .NET Core Class Library solution.

It works with or without ASP.NET. The ASP.NET parts will be moved to the NLog.Web package in the future