代码改变世界

.Netcore IOptions<LoggerFilterOptions> 获取的顺序

2022-11-04 13:53  qgbo  阅读(26)  评论(0编辑  收藏  举报

.net core 配置文件的日志级别 :

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Trace",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

IOptions<LoggerFilterOptions>  这个会读取这个节点。

然而用的时候发现,这个节点下的 key 会自动排序,也就是 Microsoft.Hosting.Lifetime 一直是第一个,调整配置文件的顺序也没用。

这当然用起来方便。

然而 这个源码,并没有排序。

这个加的时候,用 一个 AsEnumable() 方法

获取 GetChilderen()  在这儿执行。

这会指向这个方法:GetChildrenImplementation

这里又有 

IEnumerable<IConfigurationSection> children = providers
                .Aggregate(Enumerable.Empty<string>(),
                    (seed, source) => source.GetChildKeys(seed, path))
                .Distinct(StringComparer.OrdinalIgnoreCase)
                .Select(key => root.GetSection(path == null ? key : ConfigurationPath.Combine(path, key)));
GetChildKeys 这个方法的实现在这儿
这个方法 的最后 有个 results.Sort(ConfigurationKeyComparer.Comparison);