平安校园
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.
 
 
 
 
 
 

148 lines
5.0 KiB

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