|
| 1 | +namespace dotnetCampus.Logging; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// 辅助将字符串解析为日志级别。 |
| 5 | +/// </summary> |
| 6 | +public static class LogLevelParser |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// 尝试解析字符串为日志级别,支持常用的日志级别别名,大小写不敏感。 |
| 10 | + /// </summary> |
| 11 | + /// <param name="text">要解析的字符串。</param> |
| 12 | + /// <returns>日志级别。</returns> |
| 13 | + /// <remarks> |
| 14 | + /// 目前已支持的别名有: |
| 15 | + /// <list type="bullet"> |
| 16 | + /// <item><description>追踪级:0, trace, tracing</description></item> |
| 17 | + /// <item><description>调试级:1, debug, debugging</description></item> |
| 18 | + /// <item><description>一般级:2, info, information</description></item> |
| 19 | + /// <item><description>警告级:3, warn, warning</description></item> |
| 20 | + /// <item><description>错误级:4, err, error</description></item> |
| 21 | + /// <item><description>崩溃级:5, critical, fatal</description></item> |
| 22 | + /// <item><description>无日志:6, no, none</description></item> |
| 23 | + /// </list> |
| 24 | + /// 其他所有字符串均返回 <see langword="null"/>。 |
| 25 | + /// </remarks> |
| 26 | + public static LogLevel? Parse(string text) => text.ToLowerInvariant() switch |
| 27 | + { |
| 28 | + "trace" => LogLevel.Trace, |
| 29 | + "tracing" => LogLevel.Trace, |
| 30 | + "debug" => LogLevel.Debug, |
| 31 | + "debugging" => LogLevel.Debug, |
| 32 | + "info" => LogLevel.Information, |
| 33 | + "information" => LogLevel.Information, |
| 34 | + "warn" => LogLevel.Warning, |
| 35 | + "warning" => LogLevel.Warning, |
| 36 | + "err" => LogLevel.Error, |
| 37 | + "error" => LogLevel.Error, |
| 38 | + "critical" => LogLevel.Critical, |
| 39 | + "fatal" => LogLevel.Critical, |
| 40 | + "no" => LogLevel.None, |
| 41 | + "none" => LogLevel.None, |
| 42 | + "0" => LogLevel.Trace, |
| 43 | + "1" => LogLevel.Debug, |
| 44 | + "2" => LogLevel.Information, |
| 45 | + "3" => LogLevel.Warning, |
| 46 | + "4" => LogLevel.Error, |
| 47 | + "5" => LogLevel.Critical, |
| 48 | + "6" => LogLevel.None, |
| 49 | + _ => null, |
| 50 | + }; |
| 51 | +} |
0 commit comments