简体   繁体   中英

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

Why does dot net Core create a folder called "netcpreapp3.1" inside Debug and how can I dynamically get to it? I have added log4net to my dot net Core Web API app, and if I put the log4net.config at the same level as the Startup.cs file, and then in Program.cs I add the ConfigureLogging part like this:

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

it's all fine, but if I put log4net.config inside of a folder called Config, it is no longer found until I do:

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

(not great, because we'll have netcoreapp3.2 in the future, right?) and then it breaks somewhere else: loggerFactory.AddLog4Net(); // in Startup.cs, saying it expects \bin\Debug\netcoreapp3.1\log4net.config (my Config folder is not there in the path). PS: This implementation is using: using Microsoft.Extensions.Logging;

So, question is how can I move log4net.config in a non standard place - which isn't same level as Startup.cs?

You can use "Config\log4net.config" as the path, no need to add the "netcoreapp3.1" part of the path as that's where the process is started, it's the working path.

So, you need to specify that path to the builder.AddLog4Net call and also to the loggerFactory.AddLog4Net .

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