平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoggingConsoleComponent.cs 5.0 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 
  2. //
  3. namespace SafeCampus.Core;
  4. /// <summary>
  5. /// 日志写入文件的组件
  6. /// </summary>
  7. public sealed class LoggingConsoleComponent : IServiceComponent
  8. {
  9. private readonly LoggingSetting _loggingSetting = App.GetConfig<LoggingSetting>("Logging", true);
  10. private readonly string _monitorName = "System.Logging.LoggingMonitor";
  11. public void Load(IServiceCollection services, ComponentContext componentContext)
  12. {
  13. services.AddConsoleFormatter(options =>
  14. {
  15. options.MessageFormat = logMsg =>
  16. {
  17. //如果不是LoggingMonitor日志或者开启了格式化才格式化
  18. if (logMsg.LogName != _monitorName && _loggingSetting.MessageFormat)
  19. {
  20. var stringBuilder = new StringBuilder();
  21. stringBuilder.AppendLine("【日志级别】:" + logMsg.LogLevel);
  22. stringBuilder.AppendLine("【日志类名】:" + logMsg.LogName);
  23. stringBuilder.AppendLine("【日志时间】:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  24. stringBuilder.AppendLine("【日志内容】:" + logMsg.Message);
  25. if (logMsg.Exception != null)
  26. {
  27. stringBuilder.AppendLine("【异常信息】:" + logMsg.Exception);
  28. }
  29. return stringBuilder.ToString();
  30. }
  31. return logMsg.Message;
  32. };
  33. options.WriteHandler = (logMsg, scopeProvider, writer,
  34. fmtMsg, opt) =>
  35. {
  36. if (logMsg.LogName == _monitorName && !_loggingSetting.Monitor.Console) return;
  37. var consoleColor = ConsoleColor.White;
  38. switch (logMsg.LogLevel)
  39. {
  40. case LogLevel.Information:
  41. consoleColor = ConsoleColor.DarkGreen;
  42. break;
  43. case LogLevel.Warning:
  44. consoleColor = ConsoleColor.DarkYellow;
  45. break;
  46. case LogLevel.Error:
  47. consoleColor = ConsoleColor.DarkRed;
  48. break;
  49. }
  50. writer.WriteWithColor(fmtMsg, ConsoleColor.Black, consoleColor);
  51. };
  52. });
  53. }
  54. }
  55. public static class TextWriterExtensions
  56. {
  57. private const string DEFAULT_FOREGROUND_COLOR = "\x1B[39m\x1B[22m";
  58. private const string DEFAULT_BACKGROUND_COLOR = "\x1B[49m";
  59. public static void WriteWithColor(this TextWriter textWriter, string message, ConsoleColor? background,
  60. ConsoleColor? foreground)
  61. {
  62. // Order:
  63. // 1. background color
  64. // 2. foreground color
  65. // 3. message
  66. // 4. reset foreground color
  67. // 5. reset background color
  68. var backgroundColor = background.HasValue ? GetBackgroundColorEscapeCode(background.Value) : null;
  69. var foregroundColor = foreground.HasValue ? GetForegroundColorEscapeCode(foreground.Value) : null;
  70. if (backgroundColor != null)
  71. {
  72. textWriter.Write(backgroundColor);
  73. }
  74. if (foregroundColor != null)
  75. {
  76. textWriter.Write(foregroundColor);
  77. }
  78. textWriter.WriteLine(message);
  79. if (foregroundColor != null)
  80. {
  81. textWriter.Write(DEFAULT_FOREGROUND_COLOR);
  82. }
  83. if (backgroundColor != null)
  84. {
  85. textWriter.Write(DEFAULT_BACKGROUND_COLOR);
  86. }
  87. }
  88. private static string GetForegroundColorEscapeCode(ConsoleColor color)
  89. {
  90. return color switch
  91. {
  92. ConsoleColor.Black => "\x1B[30m",
  93. ConsoleColor.DarkRed => "\x1B[31m",
  94. ConsoleColor.DarkGreen => "\x1B[32m",
  95. ConsoleColor.DarkYellow => "\x1B[33m",
  96. ConsoleColor.DarkBlue => "\x1B[34m",
  97. ConsoleColor.DarkMagenta => "\x1B[35m",
  98. ConsoleColor.DarkCyan => "\x1B[36m",
  99. ConsoleColor.Gray => "\x1B[37m",
  100. ConsoleColor.Red => "\x1B[1m\x1B[31m",
  101. ConsoleColor.Green => "\x1B[1m\x1B[32m",
  102. ConsoleColor.Yellow => "\x1B[1m\x1B[33m",
  103. ConsoleColor.Blue => "\x1B[1m\x1B[34m",
  104. ConsoleColor.Magenta => "\x1B[1m\x1B[35m",
  105. ConsoleColor.Cyan => "\x1B[1m\x1B[36m",
  106. ConsoleColor.White => "\x1B[1m\x1B[37m",
  107. _ => DEFAULT_FOREGROUND_COLOR
  108. };
  109. }
  110. private static string GetBackgroundColorEscapeCode(ConsoleColor color)
  111. {
  112. return color switch
  113. {
  114. ConsoleColor.Black => "\x1B[40m",
  115. ConsoleColor.DarkRed => "\x1B[41m",
  116. ConsoleColor.DarkGreen => "\x1B[42m",
  117. ConsoleColor.DarkYellow => "\x1B[43m",
  118. ConsoleColor.DarkBlue => "\x1B[44m",
  119. ConsoleColor.DarkMagenta => "\x1B[45m",
  120. ConsoleColor.DarkCyan => "\x1B[46m",
  121. ConsoleColor.Gray => "\x1B[47m",
  122. _ => DEFAULT_BACKGROUND_COLOR
  123. };
  124. }
  125. }