@@ -17,6 +17,9 @@ class ConfigurationReader : IConfigurationReader
17
17
{
18
18
const string LevelSwitchNameRegex = @"^\${0,1}[A-Za-z]+[A-Za-z0-9]*$" ;
19
19
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
+
20
23
readonly IConfiguration _section ;
21
24
readonly IReadOnlyCollection < Assembly > _configurationAssemblies ;
22
25
readonly ResolutionContext _resolutionContext ;
@@ -164,7 +167,7 @@ void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
164
167
{
165
168
var overridePrefix = overrideDirective . Key ;
166
169
var overridenLevelOrSwitch = overrideDirective . Value ;
167
- if ( Enum . TryParse ( overridenLevelOrSwitch , out LogEventLevel _ ) )
170
+ if ( Enum . TryParse ( overridenLevelOrSwitch , ignoreCase : true , out LogEventLevel _ ) )
168
171
{
169
172
ApplyMinimumLevelConfiguration ( overrideDirective , ( configuration , levelSwitch ) =>
170
173
{
@@ -221,7 +224,7 @@ void SubscribeToLoggingLevelChanges(IConfigurationSection levelSection, LoggingL
221
224
levelSection . GetReloadToken ,
222
225
( ) =>
223
226
{
224
- if ( Enum . TryParse ( levelSection . Value , out LogEventLevel minimumLevel ) )
227
+ if ( Enum . TryParse ( levelSection . Value , ignoreCase : true , out LogEventLevel minimumLevel ) )
225
228
levelSwitch . MinimumLevel = minimumLevel ;
226
229
else
227
230
SelfLog . WriteLine ( $ "The value { levelSection . Value } is not a valid Serilog level.") ;
@@ -383,7 +386,10 @@ static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfiguration
383
386
assemblies . Add ( assumed ) ;
384
387
}
385
388
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 )
387
393
{
388
394
var message = $ """
389
395
No { usingSection . Path } configuration section is defined and no Serilog assemblies were found.
@@ -591,9 +597,7 @@ internal static bool IsValidSwitchName(string input)
591
597
}
592
598
593
599
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.") ;
599
603
}
0 commit comments