Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

nlog

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?

like image 817
Craig Avatar asked Nov 16 '25 14:11

Craig


2 Answers

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.

like image 102
Charles Avatar answered Nov 18 '25 05:11

Charles


Although you are correct about App.config there are other options, for example I have a .net 6 application that uses Nlog. The config file I use is called NLog.config (and NLog.dev.config). Full details are on the NLog Configuraiton file documentation https://github.com/NLog/NLog/wiki/Configuration-file

like image 43
Steve Avatar answered Nov 18 '25 06:11

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!