@@ -80,8 +80,8 @@ Dictionary<string, Dictionary<string, string>> GetMethodCalls(IConfigurationSect
80
80
else
81
81
{
82
82
var name = child . GetSection ( "Name" ) ;
83
- if ( name == null )
84
- throw new InvalidOperationException ( "The configuration value in Serilog.WriteTo has no Name element." ) ;
83
+ if ( name . Value == null )
84
+ throw new InvalidOperationException ( $ "The configuration value in { name . Path } has no Name element.") ;
85
85
86
86
var callArgs = new Dictionary < string , string > ( ) ;
87
87
var args = child . GetSection ( "Args" ) ;
@@ -100,25 +100,39 @@ Dictionary<string, Dictionary<string, string>> GetMethodCalls(IConfigurationSect
100
100
101
101
void ApplyMinimumLevel ( LoggerConfiguration loggerConfiguration )
102
102
{
103
+ var applyMinimumLevelAction =
104
+ new Action < IConfigurationSection , Action < LoggerMinimumLevelConfiguration , LoggingLevelSwitch > > (
105
+ ( directive , applyConfigAction ) =>
106
+ {
107
+ LogEventLevel minimumLevel ;
108
+ if ( ! Enum . TryParse ( directive . Value , out minimumLevel ) )
109
+ throw new InvalidOperationException ( $ "The value { directive . Value } is not a valid Serilog level.") ;
110
+
111
+ var levelSwitch = new LoggingLevelSwitch ( minimumLevel ) ;
112
+ applyConfigAction ( loggerConfiguration . MinimumLevel , levelSwitch ) ;
113
+
114
+ ChangeToken . OnChange (
115
+ directive . GetReloadToken ,
116
+ ( ) =>
117
+ {
118
+ if ( Enum . TryParse ( directive . Value , out minimumLevel ) )
119
+ levelSwitch . MinimumLevel = minimumLevel ;
120
+ else
121
+ SelfLog . WriteLine ( $ "The value { directive . Value } is not a valid Serilog level.") ;
122
+ } ) ;
123
+ } ) ;
124
+
103
125
var minimumLevelDirective = _configuration . GetSection ( "MinimumLevel" ) ;
104
- if ( minimumLevelDirective ? . Value != null )
105
- {
106
- LogEventLevel minimumLevel ;
107
- if ( ! Enum . TryParse ( minimumLevelDirective . Value , out minimumLevel ) )
108
- throw new InvalidOperationException ( $ "The value { minimumLevelDirective . Value } is not a valid Serilog level.") ;
109
126
110
- var levelSwitch = new LoggingLevelSwitch ( minimumLevel ) ;
111
- loggerConfiguration . MinimumLevel . ControlledBy ( levelSwitch ) ;
127
+ var defaultMinLevelDirective = minimumLevelDirective . Value != null ? minimumLevelDirective : minimumLevelDirective . GetSection ( "Default" ) ;
128
+ if ( defaultMinLevelDirective . Value != null )
129
+ {
130
+ applyMinimumLevelAction ( defaultMinLevelDirective , ( configuration , levelSwitch ) => configuration . ControlledBy ( levelSwitch ) ) ;
131
+ }
112
132
113
- ChangeToken . OnChange (
114
- ( ) => minimumLevelDirective . GetReloadToken ( ) ,
115
- ( ) =>
116
- {
117
- if ( Enum . TryParse ( minimumLevelDirective . Value , out minimumLevel ) )
118
- levelSwitch . MinimumLevel = minimumLevel ;
119
- else
120
- SelfLog . WriteLine ( $ "The value { minimumLevelDirective . Value } is not a valid Serilog level.") ;
121
- } ) ;
133
+ foreach ( var overrideDirective in minimumLevelDirective . GetSection ( "Override" ) . GetChildren ( ) )
134
+ {
135
+ applyMinimumLevelAction ( overrideDirective , ( configuration , levelSwitch ) => configuration . Override ( overrideDirective . Key , levelSwitch ) ) ;
122
136
}
123
137
}
124
138
0 commit comments