Failed to publish library that uses NLog for NuGet

I am trying to publish a C # library to NuGet using NuGet Package Explorer. The library uses NLog for logging. My local version works as expected.

I can successfully download and load the library, but when I try to use the version downloaded from NuGet I get NullReferenceException

. Further experiments show that this is caused LogManager.Configuration



This is where I am trying to use LogManager.Configuration


var logger = LogManager.GetLogger("MyLibraryName");
var logTarget = new ColoredConsoleTarget();
var loggingRule = new LoggingRule("*", LogLevel.Info, logTarget);
logTarget.Layout = "${longdate} ${level} ${logger}: ${message} ${exception:format=tostring}";
LogManager.Configuration.LoggingRules.Add(loggingRule); // exception thrown here


It seems that my NLog.config file is automatically used by my local version of my library, but not the loaded version.

In NuGet Package Explorer, my "Package Content" looks like this:

- lib
  - net45
    - NLog.config
    - MyLibrary.dll


I tried renaming NLog.config to nlog.dll.nlog as recommended on the NLog wiki ( ). This didn't fix the problem.

What can I do to stop the LogManager.Configuration from null?


source to share

1 answer

First, NLog will only look into the app.config for its settings. It will not look for a .config file named after a DLL. (edit: this is the default behavior for .NET apps, but the wiki you specified indicates a different behavior - I'll pass whether they support it or not, but I'll say the solution I provide below should always work)

Second, you add physical files to your package when you have to add an NLog dependency to the package.

To fix the first part, you will need to specify some config file conversions in your pacakge. The docs can be found here . Just create a bare file app.config, rename it app.config.transform, and then add all the NLog parts to that file. Include it in your package content.

To fix the second part, remove the NLog DLL from your content. In the metadata for your package, add the dependency for NLog. You can add it manually by editing the XML

    <group targetFramework=".NETFramework4.5">
        <dependency id="NLog" version="" />


or by editing the package using NuGet Package Explorer (easier, safer). More details can be found here .



All Articles