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

I want to ensure that a particular configuration property is being read from a configuration source. I was going to print out all of the configuration sources (or print out all of the configuration properties), but I can’t seem to figure out how to do that.

Can this be done?



Solution 1:[1]

From .NET Core 3.0+ you can cast your IConfiguration to a IConfigurationRoot and use the GetDebugView extension method. That will generates a human-readable view of the configuration showing where each value came from. E.g.

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

Sample output to 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)

Solution 2:[2]

Recently, I have been faced a similar problem. I found the solution to a task here: 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();
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy Cox
Solution 2 Alex Paskhin