|
| 1 | +using System; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | + |
| 4 | +namespace Bing.Logging |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// 日志(<see cref="ILogger"/>) 扩展 |
| 8 | + /// </summary> |
| 9 | + public static class LoggerExtensions |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// 基于日志级别输出日志 |
| 13 | + /// </summary> |
| 14 | + /// <param name="logger">日志</param> |
| 15 | + /// <param name="logLevel">日志级别</param> |
| 16 | + /// <param name="message">消息</param> |
| 17 | + /// <param name="args">参数</param> |
| 18 | + public static void LogWithLevel(this ILogger logger, LogLevel logLevel, string message, params object[] args) |
| 19 | + { |
| 20 | + switch (logLevel) |
| 21 | + { |
| 22 | + case LogLevel.Trace: |
| 23 | + logger.LogTrace(message, args); |
| 24 | + break; |
| 25 | + case LogLevel.Debug: |
| 26 | + logger.LogDebug(message, args); |
| 27 | + break; |
| 28 | + case LogLevel.Information: |
| 29 | + logger.LogInformation(message, args); |
| 30 | + break; |
| 31 | + case LogLevel.Warning: |
| 32 | + logger.LogWarning(message, args); |
| 33 | + break; |
| 34 | + case LogLevel.Error: |
| 35 | + logger.LogError(message, args); |
| 36 | + break; |
| 37 | + case LogLevel.Critical: |
| 38 | + logger.LogCritical(message, args); |
| 39 | + break; |
| 40 | + default: |
| 41 | + logger.LogDebug(message, args); |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// 基于日志级别输出日志 |
| 48 | + /// </summary> |
| 49 | + /// <param name="logger">日志</param> |
| 50 | + /// <param name="logLevel">日志级别</param> |
| 51 | + /// <param name="message">消息</param> |
| 52 | + /// <param name="exception">异常</param> |
| 53 | + public static void LogWithLevel(this ILogger logger, LogLevel logLevel, string message, Exception exception) |
| 54 | + { |
| 55 | + switch (logLevel) |
| 56 | + { |
| 57 | + case LogLevel.Trace: |
| 58 | + logger.LogTrace(exception, message); |
| 59 | + break; |
| 60 | + case LogLevel.Debug: |
| 61 | + logger.LogDebug(exception, message); |
| 62 | + break; |
| 63 | + case LogLevel.Information: |
| 64 | + logger.LogInformation(exception, message); |
| 65 | + break; |
| 66 | + case LogLevel.Warning: |
| 67 | + logger.LogWarning(exception, message); |
| 68 | + break; |
| 69 | + case LogLevel.Error: |
| 70 | + logger.LogError(exception, message); |
| 71 | + break; |
| 72 | + case LogLevel.Critical: |
| 73 | + logger.LogCritical(exception, message); |
| 74 | + break; |
| 75 | + default: |
| 76 | + logger.LogDebug(exception, message); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments