LoggingSetup.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. namespace Admin.NET.Core;
  5. public static class LoggingSetup
  6. {
  7. /// <summary>
  8. /// 日志注册
  9. /// </summary>
  10. /// <param name="services"></param>
  11. public static void AddLoggingSetup(this IServiceCollection services)
  12. {
  13. // 日志监听
  14. services.AddMonitorLogging(options =>
  15. {
  16. options.IgnorePropertyNames = new[] { "Byte" };
  17. options.IgnorePropertyTypes = new[] { typeof(byte[]) };
  18. });
  19. // 控制台日志
  20. var consoleLog = App.GetConfig<bool>("Logging:Monitor:ConsoleLog", true);
  21. services.AddConsoleFormatter(options =>
  22. {
  23. options.DateFormat = "yyyy-MM-dd HH:mm:ss(zzz) dddd";
  24. //options.WithTraceId = true; // 显示线程Id
  25. //options.WithStackFrame = true; // 显示程序集
  26. options.WriteFilter = (logMsg) =>
  27. {
  28. return consoleLog;
  29. };
  30. });
  31. // 日志写入文件
  32. if (App.GetConfig<bool>("Logging:File:Enabled", true))
  33. {
  34. var loggingMonitorSettings = App.GetConfig<LoggingMonitorSettings>("Logging:Monitor", true);
  35. Array.ForEach(new[] { LogLevel.Information, LogLevel.Warning, LogLevel.Error }, logLevel =>
  36. {
  37. services.AddFileLogging(options =>
  38. {
  39. options.WithTraceId = true; // 显示线程Id
  40. options.WithStackFrame = true; // 显示程序集
  41. options.FileNameRule = fileName => string.Format(fileName, DateTime.Now, logLevel.ToString()); // 每天创建一个文件
  42. options.WriteFilter = logMsg => logMsg.LogLevel == logLevel; // 日志级别
  43. options.HandleWriteError = (writeError) => // 写入失败时启用备用文件
  44. {
  45. writeError.UseRollbackFileName(Path.GetFileNameWithoutExtension(writeError.CurrentFileName) + "-oops" + Path.GetExtension(writeError.CurrentFileName));
  46. };
  47. if (loggingMonitorSettings.JsonBehavior == JsonBehavior.OnlyJson)
  48. {
  49. options.MessageFormat = LoggerFormatter.Json;
  50. // options.MessageFormat = LoggerFormatter.JsonIndented;
  51. options.MessageFormat = (logMsg) =>
  52. {
  53. var jsonString = logMsg.Context.Get("loggingMonitor");
  54. return jsonString?.ToString();
  55. };
  56. }
  57. });
  58. });
  59. }
  60. // 日志写入数据库
  61. if (App.GetConfig<bool>("Logging:Database:Enabled", true))
  62. {
  63. services.AddDatabaseLogging<DatabaseLoggingWriter>(options =>
  64. {
  65. options.WithTraceId = true; // 显示线程Id
  66. options.WithStackFrame = true; // 显示程序集
  67. options.IgnoreReferenceLoop = false; // 忽略循环检测
  68. options.WriteFilter = (logMsg) =>
  69. {
  70. return logMsg.LogName == "System.Logging.LoggingMonitor"; // 只写LoggingMonitor日志
  71. };
  72. });
  73. }
  74. // 日志写入ElasticSearch
  75. if (App.GetConfig<bool>("Logging:ElasticSearch:Enabled", true))
  76. {
  77. services.AddDatabaseLogging<ElasticSearchLoggingWriter>(options =>
  78. {
  79. options.WithTraceId = true; // 显示线程Id
  80. options.WithStackFrame = true; // 显示程序集
  81. options.IgnoreReferenceLoop = false; // 忽略循环检测
  82. options.MessageFormat = LoggerFormatter.Json;
  83. options.WriteFilter = (logMsg) =>
  84. {
  85. return logMsg.LogName == "System.Logging.LoggingMonitor"; // 只写LoggingMonitor日志
  86. };
  87. });
  88. }
  89. }
  90. }