LoggingSetup.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core;
  7. public static class LoggingSetup
  8. {
  9. /// <summary>
  10. /// 日志注册
  11. /// </summary>
  12. /// <param name="services"></param>
  13. public static void AddLoggingSetup(this IServiceCollection services)
  14. {
  15. // 日志监听
  16. services.AddMonitorLogging(options =>
  17. {
  18. options.IgnorePropertyNames = new[] { "Byte" };
  19. options.IgnorePropertyTypes = new[] { typeof(byte[]) };
  20. });
  21. // 控制台日志
  22. var consoleLog = App.GetConfig<bool>("Logging:Monitor:ConsoleLog", true);
  23. services.AddConsoleFormatter(options =>
  24. {
  25. options.DateFormat = "yyyy-MM-dd HH:mm:ss(zzz) dddd";
  26. //options.WithTraceId = true; // 显示线程Id
  27. //options.WithStackFrame = true; // 显示程序集
  28. options.WriteFilter = (logMsg) =>
  29. {
  30. return consoleLog;
  31. };
  32. });
  33. // 日志写入文件
  34. if (App.GetConfig<bool>("Logging:File:Enabled", true))
  35. {
  36. var loggingMonitorSettings = App.GetConfig<LoggingMonitorSettings>("Logging:Monitor", true);
  37. Array.ForEach(new[] { LogLevel.Information, LogLevel.Warning, LogLevel.Error }, logLevel =>
  38. {
  39. services.AddFileLogging(options =>
  40. {
  41. options.WithTraceId = true; // 显示线程Id
  42. options.WithStackFrame = true; // 显示程序集
  43. options.FileNameRule = fileName => string.Format(fileName, DateTime.Now, logLevel.ToString()); // 每天创建一个文件
  44. options.WriteFilter = logMsg => logMsg.LogLevel >= logLevel; // 日志级别
  45. options.HandleWriteError = (writeError) => // 写入失败时启用备用文件
  46. {
  47. writeError.UseRollbackFileName(Path.GetFileNameWithoutExtension(writeError.CurrentFileName) + "-oops" + Path.GetExtension(writeError.CurrentFileName));
  48. };
  49. if (loggingMonitorSettings.JsonBehavior == JsonBehavior.OnlyJson)
  50. {
  51. options.MessageFormat = LoggerFormatter.Json;
  52. // options.MessageFormat = LoggerFormatter.JsonIndented;
  53. options.MessageFormat = (logMsg) =>
  54. {
  55. var jsonString = logMsg.Context.Get("loggingMonitor");
  56. return jsonString?.ToString();
  57. };
  58. }
  59. });
  60. });
  61. }
  62. // 日志写入ElasticSearch
  63. if (App.GetConfig<bool>("Logging:ElasticSearch:Enabled", true))
  64. {
  65. services.AddDatabaseLogging<ElasticSearchLoggingWriter>(options =>
  66. {
  67. options.WithTraceId = true; // 显示线程Id
  68. options.WithStackFrame = true; // 显示程序集
  69. options.IgnoreReferenceLoop = false; // 忽略循环检测
  70. options.MessageFormat = LoggerFormatter.Json;
  71. options.WriteFilter = (logMsg) =>
  72. {
  73. return logMsg.LogName == CommonConst.SysLogCategoryName; // 只写LoggingMonitor日志
  74. };
  75. });
  76. }
  77. // 日志写入数据库
  78. if (App.GetConfig<bool>("Logging:Database:Enabled", true))
  79. {
  80. services.AddDatabaseLogging<DatabaseLoggingWriter>(options =>
  81. {
  82. options.WithTraceId = true; // 显示线程Id
  83. options.WithStackFrame = true; // 显示程序集
  84. options.IgnoreReferenceLoop = false; // 忽略循环检测
  85. options.WriteFilter = (logMsg) =>
  86. {
  87. return logMsg.LogName == CommonConst.SysLogCategoryName; // 只写LoggingMonitor日志
  88. };
  89. });
  90. }
  91. }
  92. }