如何从appsettings.json中读取日志配置在.net Core 2.2中

9
在 .net Core 2.1 中,它是这样完成的。
var loggingConfig = configuration.GetSection("Logging");
loggerFactory.AddConsole(loggingConfig);

我已将其移至ConfigureServices,现在出现以下错误:

错误CS1503:参数2:无法将'Microsoft.Extensions.Configuration.IConfigurationSection'转换为'System.Action Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions'

1个回答

22

根据这个,你可能需要更改配置应用程序的方式:

var webHost = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", 
                      optional: true, reloadOnChange: true);
            config.AddEnvironmentVariables();
        })
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
            logging.AddConsole();
            logging.AddDebug();
            logging.AddEventSourceLogger();
        })
        .UseStartup<Startup>()
        .Build();

    webHost.Run();

1 Microsoft ASP.NET Core 2.2 日志文档


1
谢谢,就这些了。 - Mirhat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接