c# - 使用依赖于 .NET 6 应用程序中的 NLog 的第三方库

标签 c# .net nlog

根据标题,我有一个以 DLL 形式提供的第三方库,这些 DLL 依赖于也提供的 NLog.dll (4.3.5)。当我尝试从 .NET 6 客户端启动该库时,出现以下异常。 “找不到方法:System.String System.AppDomainSetup.get_ConfigurationFile()”。显然,NLog 正在寻找一个配置文件,该文件在过去应该位于 App.config 文件中。在互联网上搜索似乎可以通过调用 NLog API 来提供配置文件,但我无法这样做,因为我无法访问底层 API。有人知道让这个设置工作的方法吗?

最佳答案

错误发生在NLog.Internal.Fakeables.AppDomainWrapper中,其初始化如下:

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

然后在 AppDomainWrapper ctor 中:

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

许多 NLog 构造函数都在使用这个 AppDomainWrapper.CurrentDomain ,每次使用 NLog 类时都会出错。

这是一个很好的 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

所以我对你使用这个旧版本不抱太大希望。

关于c# - 使用依赖于 .NET 6 应用程序中的 NLog 的第三方库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70538147/

相关文章:

c# - DbContext 已被释放

c# - 从序列化 XML 中的小数中删除小数点

c# - .NET 的拼写检查器

c# - 移除 X509Certificate2 对象的智能卡时的事件

c# - 嵌入式wpf文本框不接受输入

c# - 时间格式是24小时制吗?

c# - 最近用户查询

c# - 记录和累积来自不同服务器的异常的最佳方法是什么?

.net - NLog 配置 API : Using Layouts stored in variables

asp.net - 如何在 ASP.NET 应用程序中记录错误(异常)?