diff --git a/src/dotnetCampus.Logger/LogLevelParser.cs b/src/dotnetCampus.Logger/LogLevelParser.cs new file mode 100644 index 0000000..fc3cdc3 --- /dev/null +++ b/src/dotnetCampus.Logger/LogLevelParser.cs @@ -0,0 +1,51 @@ +namespace dotnetCampus.Logging; + +/// +/// 辅助将字符串解析为日志级别。 +/// +public static class LogLevelParser +{ + /// + /// 尝试解析字符串为日志级别,支持常用的日志级别别名,大小写不敏感。 + /// + /// 要解析的字符串。 + /// 日志级别。 + /// + /// 目前已支持的别名有: + /// + /// 追踪级:0, trace, tracing + /// 调试级:1, debug, debugging + /// 一般级:2, info, information + /// 警告级:3, warn, warning + /// 错误级:4, err, error + /// 崩溃级:5, critical, fatal + /// 无日志:6, no, none + /// + /// 其他所有字符串均返回 。 + /// + public static LogLevel? Parse(string text) => text.ToLowerInvariant() switch + { + "trace" => LogLevel.Trace, + "tracing" => LogLevel.Trace, + "debug" => LogLevel.Debug, + "debugging" => LogLevel.Debug, + "info" => LogLevel.Information, + "information" => LogLevel.Information, + "warn" => LogLevel.Warning, + "warning" => LogLevel.Warning, + "err" => LogLevel.Error, + "error" => LogLevel.Error, + "critical" => LogLevel.Critical, + "fatal" => LogLevel.Critical, + "no" => LogLevel.None, + "none" => LogLevel.None, + "0" => LogLevel.Trace, + "1" => LogLevel.Debug, + "2" => LogLevel.Information, + "3" => LogLevel.Warning, + "4" => LogLevel.Error, + "5" => LogLevel.Critical, + "6" => LogLevel.None, + _ => null, + }; +}