diff --git a/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs b/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs index 3b86170..1385861 100644 --- a/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs +++ b/src/Serilog.Settings.Configuration/Settings/Configuration/ConfigurationReader.cs @@ -571,8 +571,21 @@ internal static bool IsValidSwitchName(string input) return Regex.IsMatch(input, LevelSwitchNameRegex); } - static LogEventLevel ParseLogEventLevel(string value) - => Enum.TryParse(value, ignoreCase: true, out LogEventLevel parsedLevel) - ? parsedLevel - : throw new InvalidOperationException($"The value {value} is not a valid Serilog level."); + internal static LogEventLevel ParseLogEventLevel(string value) + { + // Try parsing as LevelAlias first (handles "Off", "Minimum", "Maximum") + if (string.Equals(value, "Off", StringComparison.OrdinalIgnoreCase)) + return LevelAlias.Off; + if (string.Equals(value, "Minimum", StringComparison.OrdinalIgnoreCase)) + return LevelAlias.Minimum; + if (string.Equals(value, "Maximum", StringComparison.OrdinalIgnoreCase)) + return LevelAlias.Maximum; + + // Try parsing as LogEventLevel enum + if (Enum.TryParse(value, ignoreCase: true, out LogEventLevel parsedLevel)) + return parsedLevel; + + throw new InvalidOperationException($"The value {value} is not a valid Serilog level."); + } + } diff --git a/test/Serilog.Settings.Configuration.Tests/ConfigurationReaderTests.cs b/test/Serilog.Settings.Configuration.Tests/ConfigurationReaderTests.cs index da01316..be3c8c8 100644 --- a/test/Serilog.Settings.Configuration.Tests/ConfigurationReaderTests.cs +++ b/test/Serilog.Settings.Configuration.Tests/ConfigurationReaderTests.cs @@ -294,4 +294,39 @@ public void NoConfigurationRootUsedStillValid() AssertLogEventLevels(loggerConfig, LogEventLevel.Error); } + + [Theory] + // Standard LogEventLevel enum values + [InlineData("Verbose", LogEventLevel.Verbose)] + [InlineData("Debug", LogEventLevel.Debug)] + [InlineData("Information", LogEventLevel.Information)] + [InlineData("Warning", LogEventLevel.Warning)] + [InlineData("Error", LogEventLevel.Error)] + [InlineData("Fatal", LogEventLevel.Fatal)] + // Case insensitivity + [InlineData("verbose", LogEventLevel.Verbose)] + [InlineData("INFORMATION", LogEventLevel.Information)] + [InlineData("warning", LogEventLevel.Warning)] + // LevelAlias values + [InlineData("Off", LevelAlias.Off)] + [InlineData("off", LevelAlias.Off)] + [InlineData("OFF", LevelAlias.Off)] + [InlineData("Minimum", LevelAlias.Minimum)] + [InlineData("minimum", LevelAlias.Minimum)] + [InlineData("Maximum", LevelAlias.Maximum)] + [InlineData("maximum", LevelAlias.Maximum)] + public void ParseLogEventLevelHandlesAllLevelValues(string value, LogEventLevel expected) + { + var result = ConfigurationReader.ParseLogEventLevel(value); + Assert.Equal(expected, result); + } + + [Theory] + [InlineData("InvalidLevel")] + [InlineData("")] + [InlineData("None")] + public void ParseLogEventLevelThrowsForInvalidValues(string value) + { + Assert.Throws(() => ConfigurationReader.ParseLogEventLevel(value)); + } }