简体   繁体   中英

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?

Although you are correct about App.config there are other options, for exampleI 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM