.NET Standard 2.0 logging NLOG gives System.TypeInitializationException

4.4k Views Asked by At

I did 2 projects to test out NLog on a .NET Framework 4.6.1 standard console app and on .NET Standard 2.0 Library. My intention is to port as much code as I can to .NET Standard 2.0 for future multiplatform compatibility.

Both share the same code but the .NET Standard version produces an exception.

Here's the code

Console.WriteLine("Writing log");
Logger _errorLog = LogManager.GetLogger("ErrorsLogger");
Logger _tradesLog = LogManager.GetLogger("TradesLogger");
_errorLog.Error("This is the log message!!!");
Console.WriteLine("End log");
Console.Read();

Here's the App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
      <target name="ErrorsLogger" xsi:type="File" fileName="ErrorsLog.txt" />
      <target name="TradesLogger" xsi:type="File" fileName="TradesLog.txt" />
    </targets>

    <rules>
      <logger name="ErrorsLogger" minlevel="Info" writeTo="ErrorsLogger" />
      <logger name="TradesLogger" minlevel="Info" writeTo="TradesLogger" />
    </rules>
  </nlog>
</configuration>

I get the log fine in the .NET 4.6.1 Console app and produces the expected log file with the log message in it.

If I run the .NET Standard 2.0 library through some Microsoft Unit Test project I get this exception when it tries to call _errorLog.GetLogger

UnitTestProject.UnitTest1.TestMethod1 threw exception: System.TypeInitializationException: The type initializer for 'NLog.LogManager' threw an exception. ---> System.TypeInitializationException: The type initializer for 'NLog.LogFactory' threw an exception. ---> System.MissingMethodException: Method not found: 'System.AppDomainSetup System.AppDomain.get_SetupInformation()'.

EDIT: Nuget NLOG Version: 4.4.12

Exception thrown in a non static method:

About the non static method to get a better exception:

The exception is thrown at the GetLogger("X") level, which is a non static constructor. The initialization routine crashes even before trying to log something.

In addition, I get a WARNING (yellow exclamation mark) in the error list saying:

Warning The 'configuration' element is not declared.

Adding internal logging doesn't produce any output. This is the configuration i have used, starting from their Internal logging guide:

        InternalLogger.LogLevel = LogLevel.Trace;
        InternalLogger.LogFile = @"C:\temp\int.txt";
        InternalLogger.LogToConsole = true;
        InternalLogger.LogToConsoleError = true;
        InternalLogger.LogWriter = new StringWriter(new StringBuilder());
        InternalLogger.LogToTrace = true;
        LogManager.ThrowConfigExceptions = true;
        LogManager.ThrowExceptions = true;
        Logger logger = LogManager.GetLogger("foo");

I'm administrator and Visual Studio 2017 is started as administrator and I have permission to write in C:\temp ad .NET 4.6.1 console application is able to write in that folder and it's in the same project.

The internal log file is empty and the Unit test project runs the test successfully.

I have no clue of what is happening. No error is thrown now.

Any suggestion to debug the issue is welcome.

An ISSUE on GitHub is already opened.

HERE is a test solution that I made to show you (PASSWORD: logging123). Now that I've updated to Nlog 4.5 you will see that the .NET framework solution throws an error trying to get an old version of Nlog (that I've never referenced) and that .NET Core unit test solution works but doesn't produce any file.

2

There are 2 best solutions below

1
On BEST ANSWER

Your zipped solution is password protected, so now it is just me guessing, but it looks like you are using app.config to hold nlog.config.

Pretty sure app.config are not being used by NetCoreApps. Try to put your Nlog-config in a separate file called nlog.config and make sure it is Copy Always (In Visual Studio File Properties).

3
On

I was experiencing the same issue though I was running .NET 4.7. I updated my NLog package from 4.4.12 to 4.5.0-rc04 and it worked. Knowing its pre-release you may want to be cautions about putting it on live environment though.