@@ -42,3 +42,53 @@ public enum LogLevel
42
42
/// </summary>
43
43
None = 6 ,
44
44
}
45
+
46
+ /// <summary>
47
+ /// 辅助将字符串解析为日志级别。
48
+ /// </summary>
49
+ public static class LogLevelParser
50
+ {
51
+ /// <summary>
52
+ /// 尝试解析字符串为日志级别,支持常用的日志级别别名,大小写不敏感。
53
+ /// </summary>
54
+ /// <param name="text">要解析的字符串。</param>
55
+ /// <returns>日志级别。</returns>
56
+ /// <remarks>
57
+ /// 目前已支持的别名有:
58
+ /// <list type="bullet">
59
+ /// <item><description>追踪级:0, trace, tracing</description></item>
60
+ /// <item><description>调试级:1, debug, debugging</description></item>
61
+ /// <item><description>一般级:2, info, information</description></item>
62
+ /// <item><description>警告级:3, warn, warning</description></item>
63
+ /// <item><description>错误级:4, err, error</description></item>
64
+ /// <item><description>崩溃级:5, critical, fatal</description></item>
65
+ /// <item><description>无日志:6, no, none</description></item>
66
+ /// </list>
67
+ /// 其他所有字符串均返回 <see langword="null"/>。
68
+ /// </remarks>
69
+ public static LogLevel ? Parse ( string text ) => text . ToLowerInvariant ( ) switch
70
+ {
71
+ "trace" => LogLevel . Trace ,
72
+ "tracing" => LogLevel . Trace ,
73
+ "debug" => LogLevel . Debug ,
74
+ "debugging" => LogLevel . Debug ,
75
+ "info" => LogLevel . Information ,
76
+ "information" => LogLevel . Information ,
77
+ "warn" => LogLevel . Warning ,
78
+ "warning" => LogLevel . Warning ,
79
+ "err" => LogLevel . Error ,
80
+ "error" => LogLevel . Error ,
81
+ "critical" => LogLevel . Critical ,
82
+ "fatal" => LogLevel . Critical ,
83
+ "no" => LogLevel . None ,
84
+ "none" => LogLevel . None ,
85
+ "0" => LogLevel . Trace ,
86
+ "1" => LogLevel . Debug ,
87
+ "2" => LogLevel . Information ,
88
+ "3" => LogLevel . Warning ,
89
+ "4" => LogLevel . Error ,
90
+ "5" => LogLevel . Critical ,
91
+ "6" => LogLevel . None ,
92
+ _ => null ,
93
+ } ;
94
+ }
0 commit comments