nlog.config- 找不到文件 .Net core 3.1 和 Docker

nlog.config- file not found .Net core 3.1 and Docker

我是 Docker 的新手,我在 Main 方法的下面一行收到错误消息,它是一个带有 Docker 的 .net core 3.1 API, 当它尝试初始化 Nlog 时,代码无法找到 nlog.config,文件配置为始终复制。

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

这是我遇到的错误,

System.IO.FileNotFoundException: 'Failed to load NLog LoggingConfiguration. 
  Searched the following locations:
     - /app/bin/Debug/netcoreapp3.1/nlog.config

在 运行 IIS 下工作正常。

提前致谢 阿比德

根据你提供的信息很难判断。您是否有任何其他信息 - 您可以 provide/show 相关行的 Dockerfile? docker在 docker examples 上 Asp.Net 核心应用程序有一些指导原则。

如果您的应用程序可以在 IIS 中运行但不能在 docker 中运行,那么我建议您的 nlog.config 文件不是容器的一部分。也许在构建过程中检查你的 nlog.config 文件是否被复制到输出目录? (在VS2019中右键单击文件,然后属性,然后将“复制到输出目录”设置为“如果更新则复制”)

您可以构建容器,然后使用以下 docker 命令进入 bash 会话以浏览 directories/etc:

使用以下方法构建容器:

docker build -t nlog-example .

使用以下方式输入 bash 终端会话:

docker run --rm -it --entrypoint /bin/bash nlog-example

(注意,nlog-example 是我示例中的 docker 标记,但您的可能不同)

为那些正在处理这种环境的人添加一个额外的答案: Docker + Linux:

On Linux, the file system is case sensitive一样,让我们​​简单地将配置名称更改为与您在图片中看到的完全相同,区分大小写。

但另一方面,如果重命名不起作用,设置LogManager.LogFactory.SetCandidateConfigFilePaths也是必要的。

这里我的配置名称是NLog.Config:

var fullPath = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location))
        .FirstOrDefault(x => x.EndsWith("nlog.config", StringComparison.OrdinalIgnoreCase));

LogManager.LogFactory.SetCandidateConfigFilePaths(new[] { fullPath });
var config = Path.GetFileName(fullPath);
return NLogBuilder.ConfigureNLog(config).GetCurrentClassLogger();