Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidOperationException>(() => ConfigurationReader.ParseLogEventLevel(value));
}
}