在非标准位置(不是解决方案的根)的点网核心中移动 log4net.config 时如何修复错误

How to fix error when moving log4net.config in a dot net Core in a non standard place (not the root of the solution)

为什么 dot net Core 在 Debug 中创建一个名为 "netcpreapp3.1" 的文件夹,我如何动态访问它? 我已将 log4net 添加到我的 dot net Core Web API 应用程序中,如果我将 log4net.config 与 Startup.cs 文件放在同一级别,然后在 Program.cs 中添加像这样的 ConfigureLogging 部分:

Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            }).ConfigureLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddLog4Net("log4net.config");
            });

一切都很好,但是如果我将 log4net.config 放入名为 Config 的文件夹中,则在我这样做之前找不到它:

builder.AddLog4Net("..\netcoreapp3.1\Config\log4net.config");

(不太好,因为我们将来会有 netcoreapp3.2,对吧?) 然后它在其他地方中断:loggerFactory.AddLog4Net(); // in Startup.cs,说它期望 \bin\Debug\netcoreapp3.1\log4net.config(我的 Config 文件夹不在路径中)。 PS:此实现使用:使用 Microsoft.Extensions.Logging;

所以,问题是如何将 log4net.config 移动到非标准位置 - 与 Startup.cs 不在同一层?

您可以使用 "Config\log4net.config" 作为路径,无需添加路径的 "netcoreapp3.1" 部分,因为那是进程开始的地方,它是工作路径。

因此,您需要指定 builder.AddLog4Net 调用以及 loggerFactory.AddLog4Net.

的路径