簡體   English   中英

"如何列出 ASP.NET Core 中的所有配置源或屬性?"

[英]How can I list all of the configuration sources or properties in ASP.NET Core?

我想確保從配置源中讀取特定的配置屬性。 我打算打印出所有配置源(或打印出所有配置屬性),但我似乎無法弄清楚如何做到這一點。

這可以做到嗎?

從 .NET Core 3.0+ 開始,您可以將IConfiguration轉換為IConfigurationRoot並使用GetDebugView擴展方法。 這將生成一個人類可讀的配置視圖,顯示每個值的來源。 例如

var root = (IConfigurationRoot)Configuration;
var debugView = root.GetDebugView();

debugView示例輸出:

applicationName=Project.Name (Microsoft.Extensions.Configuration.ChainedConfigurationProvider)
ASPNETCORE_ENVIRONMENT=Development (EnvironmentVariablesConfigurationProvider)
ASPNETCORE_HTTPS_PORT=32774 (EnvironmentVariablesConfigurationProvider)
ASPNETCORE_LOGGING:
  CONSOLE:
    DISABLECOLORS=true (EnvironmentVariablesConfigurationProvider)
ASPNETCORE_URLS=https://+:443;http://+:80 (EnvironmentVariablesConfigurationProvider)
DOTNET_RUNNING_IN_CONTAINER=true (EnvironmentVariablesConfigurationProvider)
DOTNET_USE_POLLING_FILE_WATCHER=1 (EnvironmentVariablesConfigurationProvider)
AllowedHosts=* (JsonConfigurationProvider for 'appsettings.json' (Required))
Kestrel:
  Certificates:
    Development:
      Password=xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxx (JsonConfigurationProvider for 'secrets.json' (Optional))
EmailOptions:
  EnableSsl=False (JsonConfigurationProvider for 'appsettings.json' (Required))
ENVIRONMENT=Development (Microsoft.Extensions.Configuration.ChainedConfigurationProvider)
HOME=/root (EnvironmentVariablesConfigurationProvider)
HOSTNAME=2cb0f5c24cc0 (EnvironmentVariablesConfigurationProvider)
HTTPS_PORT=32774 (Microsoft.Extensions.Configuration.ChainedConfigurationProvider)
NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages;/root/.nuget/fallbackpackages2 (EnvironmentVariablesConfigurationProvider)
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin (EnvironmentVariablesConfigurationProvider)
PWD=/app (EnvironmentVariablesConfigurationProvider)
RUNNING_IN_CONTAINER=true (Microsoft.Extensions.Configuration.ChainedConfigurationProvider)
URLS=https://+:443;http://+:80 (Microsoft.Extensions.Configuration.ChainedConfigurationProvider)
USE_POLLING_FILE_WATCHER=1 (Microsoft.Extensions.Configuration.ChainedConfigurationProvider)

您可以通過執行以下操作獲取所有配置源發現的所有密鑰的列表:

var keys = builder.Build().AsEnumerable().ToList();

我還沒有找到單獨構建每個配置源的方法,因此您可以單獨查看源。

在調試模式下,您可以看到私有成員並查看每個配置源:

調試配置提供程序

最近,我遇到了類似的問題。 我在這里找到了一項任務的解決方案: https<\/a> :\/\/github.com\/Wallsmedia\/DotNetCore.Configuration.Formatter

    public static List<string> AllConfigurationKeys(this IConfigurationRoot root)
    {
        (string Value, IConfigurationProvider Provider) GetValueAndProvider(
                                                            IConfigurationRoot root,
                                                            string key)
        {
            foreach (IConfigurationProvider provider in root.Providers.Reverse())
            {
                if (provider.TryGet(key, out string value))
                {
                    return (value, provider);
                }
            }

            return (null, null);
        }

        void RecurseChildren(
                HashSet<string> keys,
                IEnumerable<IConfigurationSection> children, string rootPath)
        {
            foreach (IConfigurationSection child in children)
            {
                (string Value, IConfigurationProvider Provider) valueAndProvider = GetValueAndProvider(root, child.Path);

                if (valueAndProvider.Provider != null)
                {
                    keys.Add(rootPath + ":" + child.Key);
                }

                RecurseChildren(keys, child.GetChildren(), child.Path);
            }
        }

        var keys = new HashSet<string>();
        RecurseChildren(keys, root.GetChildren(), "");
        return keys.ToList();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM