Search code examples
c#.netnlog

Using a third party library which depends on NLog from a .NET 6 application


As per the title, I have a third party library supplied in the form of DLLs, these DLLs have a dependency on NLog.dll (4.3.5) which is also supplied. When I try and start the library from a .NET 6 client I get the following exception. "Method not found: System.String System.AppDomainSetup.get_ConfigurationFile()". Apparently NLog is looking for a config file which in the past would have been in the App.config file. Searching the internet it appears one can supply the config file via a call to the NLog API, but I can't do this as I have no access to the underlying API. Does anybody know of a way of getting this setup working?


Solution

  • The error occurs in NLog.Internal.Fakeables.AppDomainWrapper which is initialized like this:

    public static AppDomainWrapper CurrentDomain => new AppDomainWrapper(AppDomain.CurrentDomain);
    

    Then later in AppDomainWrapper ctor:

    public AppDomainWrapper(AppDomain appDomain)
    {
        BaseDirectory = appDomain.BaseDirectory;
        // SetupInformation.ConfigurationFile doesn't exist in .NET 6
        ConfigurationFile = appDomain.SetupInformation.ConfigurationFile;
    

    Many Constructors of NLog are using this AppDomainWrapper.CurrentDomain which will Error every time you use a NLog class.

    Here is a nice StackTrace:

    at NLog.Internal.Fakeables.AppDomainWrapper..ctor(AppDomain appDomain) in NLog\Internal\Fakeables\AppDomainWrapper.cs:line 58
    at NLog.Internal.Fakeables.AppDomainWrapper.get_CurrentDomain()
    at NLog.LogFactory.get_CurrentAppDomain() in NLog\LogFactory.cs:line 154
    at NLog.LogFactory..ctor() in NLog\LogFactory.cs:line 316
    at NLog.LogManager..cctor() in NLog\LogManager.cs:line 133
    

    So i don't have much hope for you using this old version.