Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 234 Vote(s) - 3.35 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I list all of the configuration sources or properties in ASP.NET Core?

#1
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?
Reply

#2
You can get a list of all the keys discovered by all configuration sources by doing:

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

I haven't found a way to build each configuration source separately so you could see the sources individually.

In debug mode, you can see the private members and peek into each configuration source:

[![Debug configuration providers][1]][1]


[1]:
Reply

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

```csharp
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)
```

[1]:

[To see links please register here]

Reply

#4
Recently, I have been faced a similar problem.
I found the solution to a task here:

[To see links please register here]


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();
}


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through