Skip to content

Commit 2598d22

Browse files
Bánhegyi ÁlmosBánhegyi Álmos
authored andcommitted
Minimumlevel partial fix with IConfigurationRoot.Providers
A partial fix for minimumlevel not being correctly overriden by higher priority providers.
1 parent 0624f63 commit 2598d22

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ class ConfigurationReader : IConfigurationReader
2222
readonly IConfigurationSection _section;
2323
readonly IReadOnlyCollection<Assembly> _configurationAssemblies;
2424
readonly ResolutionContext _resolutionContext;
25+
#if NETSTANDARD || NET461
26+
readonly IConfigurationRoot _configurationRoot;
27+
#endif
2528

2629
public ConfigurationReader(IConfigurationSection configSection, AssemblyFinder assemblyFinder, IConfiguration configuration = null)
2730
{
2831
_section = configSection ?? throw new ArgumentNullException(nameof(configSection));
2932
_configurationAssemblies = LoadConfigurationAssemblies(_section, assemblyFinder);
3033
_resolutionContext = new ResolutionContext(configuration);
34+
#if NETSTANDARD || NET461
35+
_configurationRoot = configuration as IConfigurationRoot;
36+
#endif
37+
3138
}
3239

3340
// Used internally for processing nested configuration sections -- see GetMethodCalls below.
@@ -85,7 +92,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
8592
{
8693
var minimumLevelDirective = _section.GetSection("MinimumLevel");
8794

88-
var defaultMinLevelDirective = minimumLevelDirective.Value != null ? minimumLevelDirective : minimumLevelDirective.GetSection("Default");
95+
IConfigurationSection defaultMinLevelDirective = GetDefaultMinLevelDirective();
8996
if (defaultMinLevelDirective.Value != null)
9097
{
9198
ApplyMinimumLevel(defaultMinLevelDirective, (configuration, levelSwitch) => configuration.ControlledBy(levelSwitch));
@@ -124,6 +131,35 @@ void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLeve
124131

125132
SubscribeToLoggingLevelChanges(directive, levelSwitch);
126133
}
134+
135+
IConfigurationSection GetDefaultMinLevelDirective()
136+
{
137+
#if NETSTANDARD || NET461
138+
139+
var defaultLevelDirective = minimumLevelDirective.GetSection("Default");
140+
if (_configurationRoot != null && minimumLevelDirective.Value != null && defaultLevelDirective.Value != null)
141+
{
142+
foreach (var provider in _configurationRoot.Providers.Reverse())
143+
{
144+
if (provider.TryGet(minimumLevelDirective.Path, out _))
145+
{
146+
return _configurationRoot.GetSection(minimumLevelDirective.Path);
147+
}
148+
149+
if (provider.TryGet(defaultLevelDirective.Path, out _))
150+
{
151+
return _configurationRoot.GetSection(defaultLevelDirective.Path);
152+
}
153+
}
154+
155+
return null;
156+
}
157+
158+
#endif //NET451 or fallback
159+
160+
return minimumLevelDirective.Value != null ? minimumLevelDirective : minimumLevelDirective.GetSection("Default");
161+
162+
}
127163
}
128164

129165
void SubscribeToLoggingLevelChanges(IConfigurationSection levelSection, LoggingLevelSwitch levelSwitch)

0 commit comments

Comments
 (0)