如何列出 ASP.NET Core 中的所有配置源或属性?
How can I list all of the configuration sources or properties in ASP.NET Core?
我想确保正在从配置源读取特定配置 属性。我打算打印出所有配置源(或打印出所有配置属性),但我似乎不知道该怎么做。
这可以做到吗?
您可以通过以下操作获取所有配置源发现的所有密钥的列表:
var keys = builder.Build().AsEnumerable().ToList();
我还没有找到单独构建每个配置源的方法,因此您可以单独查看源。
在调试模式下,您可以看到私有成员并查看每个配置源:
从 .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)
最近,我也遇到了类似的问题。
我在这里找到了任务的解决方案:
https://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();
}
我想确保正在从配置源读取特定配置 属性。我打算打印出所有配置源(或打印出所有配置属性),但我似乎不知道该怎么做。
这可以做到吗?
您可以通过以下操作获取所有配置源发现的所有密钥的列表:
var keys = builder.Build().AsEnumerable().ToList();
我还没有找到单独构建每个配置源的方法,因此您可以单独查看源。
在调试模式下,您可以看到私有成员并查看每个配置源:
从 .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)
最近,我也遇到了类似的问题。 我在这里找到了任务的解决方案: https://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();
}