@@ -17,6 +17,9 @@ class ConfigurationReader : IConfigurationReader
1717{
1818 const string LevelSwitchNameRegex = @"^\${0,1}[A-Za-z]+[A-Za-z0-9]*$" ;
1919
20+ // Section names that can be handled by Serilog itself (hence builtin) without requiring any additional assemblies.
21+ static readonly string [ ] BuiltinSectionNames = { "LevelSwitches" , "MinimumLevel" , "Properties" } ;
22+
2023 readonly IConfiguration _section ;
2124 readonly IReadOnlyCollection < Assembly > _configurationAssemblies ;
2225 readonly ResolutionContext _resolutionContext ;
@@ -164,7 +167,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
164167 {
165168 var overridePrefix = overrideDirective . Key ;
166169 var overridenLevelOrSwitch = overrideDirective . Value ;
167- if ( Enum . TryParse ( overridenLevelOrSwitch , out LogEventLevel _ ) )
170+ if ( Enum . TryParse ( overridenLevelOrSwitch , ignoreCase : true , out LogEventLevel _ ) )
168171 {
169172 ApplyMinimumLevelConfiguration ( overrideDirective , ( configuration , levelSwitch ) =>
170173 {
@@ -221,7 +224,7 @@ void SubscribeToLoggingLevelChanges(IConfigurationSection levelSection, LoggingL
221224 levelSection . GetReloadToken ,
222225 ( ) =>
223226 {
224- if ( Enum . TryParse ( levelSection . Value , out LogEventLevel minimumLevel ) )
227+ if ( Enum . TryParse ( levelSection . Value , ignoreCase : true , out LogEventLevel minimumLevel ) )
225228 levelSwitch . MinimumLevel = minimumLevel ;
226229 else
227230 SelfLog . WriteLine ( $ "The value { levelSection . Value } is not a valid Serilog level.") ;
@@ -383,7 +386,10 @@ static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration
383386 assemblies . Add ( assumed ) ;
384387 }
385388
386- if ( assemblies . Count == 1 )
389+ // We don't want to throw if the configuration contains only sections that can be handled by Serilog itself, without requiring any additional assembly.
390+ // See https://github.com/serilog/serilog-settings-configuration/issues/389
391+ var requiresAdditionalAssemblies = section . GetChildren ( ) . Select ( e => e . Key ) . Except ( BuiltinSectionNames ) . Any ( ) ;
392+ if ( assemblies . Count == 1 && requiresAdditionalAssemblies )
387393 {
388394 var message = $ """
389395 No { usingSection . Path } configuration section is defined and no Serilog assemblies were found.
@@ -591,9 +597,7 @@ internal static bool IsValidSwitchName(string input)
591597 }
592598
593599 static LogEventLevel ParseLogEventLevel ( string value )
594- {
595- if ( ! Enum . TryParse ( value , ignoreCase : true , out LogEventLevel parsedLevel ) )
596- throw new InvalidOperationException ( $ "The value { value } is not a valid Serilog level.") ;
597- return parsedLevel ;
598- }
600+ => Enum . TryParse ( value , ignoreCase : true , out LogEventLevel parsedLevel )
601+ ? parsedLevel
602+ : throw new InvalidOperationException ( $ "The value { value } is not a valid Serilog level.") ;
599603}
0 commit comments