/
ConfigurationRootExtensions.cs
75 lines (66 loc) · 2.51 KB
/
ConfigurationRootExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Extension methods for <see cref="IConfigurationRoot"/>.
/// </summary>
public static class ConfigurationRootExtensions
{
/// <summary>
/// Generates a human-readable view of the configuration showing where each value came from.
/// </summary>
/// <returns> The debug view. </returns>
public static string GetDebugView(this IConfigurationRoot root)
{
void RecurseChildren(
StringBuilder stringBuilder,
IEnumerable<IConfigurationSection> children,
string indent)
{
foreach (IConfigurationSection child in children)
{
(string Value, IConfigurationProvider Provider) valueAndProvider = GetValueAndProvider(root, child.Path);
if (valueAndProvider.Provider != null)
{
stringBuilder
.Append(indent)
.Append(child.Key)
.Append('=')
.Append(valueAndProvider.Value)
.Append(" (")
.Append(valueAndProvider.Provider)
.AppendLine(")");
}
else
{
stringBuilder
.Append(indent)
.Append(child.Key)
.AppendLine(":");
}
RecurseChildren(stringBuilder, child.GetChildren(), indent + " ");
}
}
var builder = new StringBuilder();
RecurseChildren(builder, root.GetChildren(), "");
return builder.ToString();
}
private static (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);
}
}
}