@@ -22,12 +22,19 @@ class ConfigurationReader : IConfigurationReader
22
22
readonly IConfigurationSection _section ;
23
23
readonly IReadOnlyCollection < Assembly > _configurationAssemblies ;
24
24
readonly ResolutionContext _resolutionContext ;
25
+ #if NETSTANDARD || NET461
26
+ readonly IConfigurationRoot _configurationRoot ;
27
+ #endif
25
28
26
29
public ConfigurationReader ( IConfigurationSection configSection , AssemblyFinder assemblyFinder , IConfiguration configuration = null )
27
30
{
28
31
_section = configSection ?? throw new ArgumentNullException ( nameof ( configSection ) ) ;
29
32
_configurationAssemblies = LoadConfigurationAssemblies ( _section , assemblyFinder ) ;
30
33
_resolutionContext = new ResolutionContext ( configuration ) ;
34
+ #if NETSTANDARD || NET461
35
+ _configurationRoot = configuration as IConfigurationRoot ;
36
+ #endif
37
+
31
38
}
32
39
33
40
// Used internally for processing nested configuration sections -- see GetMethodCalls below.
@@ -85,7 +92,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
85
92
{
86
93
var minimumLevelDirective = _section . GetSection ( "MinimumLevel" ) ;
87
94
88
- var defaultMinLevelDirective = minimumLevelDirective . Value != null ? minimumLevelDirective : minimumLevelDirective . GetSection ( "Default" ) ;
95
+ IConfigurationSection defaultMinLevelDirective = GetDefaultMinLevelDirective ( ) ;
89
96
if ( defaultMinLevelDirective . Value != null )
90
97
{
91
98
ApplyMinimumLevel ( defaultMinLevelDirective , ( configuration , levelSwitch ) => configuration . ControlledBy ( levelSwitch ) ) ;
@@ -124,6 +131,35 @@ void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLeve
124
131
125
132
SubscribeToLoggingLevelChanges ( directive , levelSwitch ) ;
126
133
}
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
+ }
127
163
}
128
164
129
165
void SubscribeToLoggingLevelChanges ( IConfigurationSection levelSection , LoggingLevelSwitch levelSwitch )
0 commit comments