简体   繁体   English

asp.net 核心日志记录不遵循 ConfigurationBuilder 层次结构

[英]asp.net core Logging not following the ConfigurationBuilder hierarchy

I'm trying to configure the log level of an asp.net core application.我正在尝试配置 asp.net 核心应用程序的日志级别。 I have this ConfigurationBuilder set up in my Startup class.我在 Startup 类中设置了这个 ConfigurationBuilder。

         var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile("ConfigFiles/appsettings.configs.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();

And then in even though I configure the LogLevel in my "ConfigFiles/appsettings.configs.json" file, like this:然后即使我在“ConfigFiles/appsettings.configs.json”文件中配置了 LogLevel,如下所示:

  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  }

It is simply being ignored.它只是被忽略了。 Instead, it is always reading directly from "appsettings.json" (the root file), no matter what.相反,它总是直接从“appsettings.json”(根文件)中读取,无论如何。 I've even tried to comment out the line of code that configures it, and surprisingly it still reads from there.我什至试图注释掉配置它的代码行,令人惊讶的是它仍然从那里读取。 I know that because I deliberately inserted a bug in the LogLevel configuration, and it throws an error.我知道这是因为我故意在 LogLevel 配置中插入了一个错误,它会引发错误。 For the other configuration it works correctly, reading from both files.对于其他配置,它可以正常工作,从两个文件中读取。 Looks like "Logging" mechanism statically points to the root appsettings.json file.看起来“日志记录”机制静态指向根 appsettings.json 文件。 How do I make it to read from the other file, instead of the root?我如何使它从另一个文件而不是根文件中读取?

Thank you谢谢

EDIT: Framework Version: .net Core 3.0编辑:框架版本:.net Core 3.0

From the docs :文档

AddJsonFile is automatically called twice when you initialize a new host builder with CreateDefaultBuilder .当您使用CreateDefaultBuilder初始化新的主机构建器时,会自动调用AddJsonFile两次。 The method is called to load configuration from:调用该方法以从以下位置加载配置:

appsettings.json – This file is read first. appsettings.json – 首先读取此文件。 The environment version of the file can override the values provided by the appsettings.json file.文件的环境版本可以覆盖 appsettings.json 文件提供的值。

appsettings.{Environment}.json – The environment version of the file is loaded based on the IHostingEnvironment.EnvironmentName. appsettings.{Environment}.json – 根据 IHostingEnvironment.EnvironmentName 加载文件的环境版本。

There are two config files appsettings.json and appsettings.Development.json which overrides settings in the base appsettings.json file.有两个配置文件appsettings.jsonappsettings.Development.json覆盖了基本appsettings.json文件中的设置。 And system use it in development mode.并且系统在开发模式下使用它。 You could change the default Loglevel in appsettings.Development.json file .您可以在appsettings.Development.json文件中更改默认appsettings.Development.json

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM