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

100 lines
3.5 KiB

  1. //
  2. //
  3. using System.Runtime.InteropServices;
  4. namespace SafeCampus.Core;
  5. /// <summary>
  6. /// 日志写入文件的组件
  7. /// </summary>
  8. public sealed class LoggingFileComponent : IServiceComponent
  9. {
  10. private readonly LoggingSetting _loggingSetting = App.GetConfig<LoggingSetting>("Logging", true);
  11. private readonly string _monitorName = "System.Logging.LoggingMonitor";
  12. public void Load(IServiceCollection services, ComponentContext componentContext)
  13. {
  14. //获取默认日志等级
  15. var defaultLevel = (LogLevel)Enum.Parse(typeof(LogLevel), _loggingSetting.LogLevel.Default);
  16. //获取最大日志等级,默认Error
  17. var maxLevel = (LogLevel)Enum.Parse(typeof(LogLevel), _loggingSetting.LogLevel.MaxLevel);
  18. //获取程序根目录
  19. // var rootPath = App.HostEnvironment.ContentRootPath;
  20. //遍历日志等级
  21. foreach (LogLevel level in Enum.GetValues(typeof(LogLevel)))
  22. {
  23. //如果日志等级是默认等级和最大等级之间
  24. if (level >= defaultLevel && level != LogLevel.None && level <= maxLevel)
  25. {
  26. //每天创建一个日志文件
  27. services.AddLogging(builder =>
  28. {
  29. var fileName = "logs/" + level + "/{0:yyyy}-{0:MM}-{0:dd}.log";
  30. builder.AddFile(fileName, options =>
  31. {
  32. SetLogOptions(options, level);//日志格式化
  33. });
  34. });
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// 日志格式化
  40. /// </summary>
  41. /// <param name="options"></param>
  42. /// <param name="logLevel"></param>
  43. private void SetLogOptions(FileLoggerOptions options, LogLevel? logLevel)
  44. {
  45. //每天创建一个日志文件
  46. var rootPath = App.HostEnvironment.ContentRootPath;
  47. if (logLevel != null)//如果日志等级不为空
  48. {
  49. //过滤日志等级
  50. options.WriteFilter = logMsg =>
  51. {
  52. //如果配置不写入monitor日志和日志名称为System.Logging.LoggingMonitor
  53. if (!_loggingSetting.Monitor.Write && logMsg.LogName == _monitorName)
  54. return false;
  55. return logMsg.LogLevel == logLevel;
  56. };
  57. }
  58. //定义日志文件名
  59. options.FileNameRule = fileName =>
  60. {
  61. var pathSeparator = @"\";
  62. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  63. pathSeparator = "/";//为linux或bsd时修改路径
  64. return rootPath + pathSeparator + string.Format(fileName, DateTime.UtcNow);
  65. };
  66. options.FileSizeLimitBytes = 500000 * 1024;//日志最大500M
  67. if (_loggingSetting.MessageFormat)
  68. {
  69. //日志内容格式化
  70. options.MessageFormat = logMsg =>
  71. {
  72. var stringBuilder = new StringBuilder();
  73. stringBuilder.AppendLine("【日志级别】:" + logMsg.LogLevel);
  74. stringBuilder.AppendLine("【日志类名】:" + logMsg.LogName);
  75. stringBuilder.AppendLine("【日志时间】:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  76. stringBuilder.AppendLine("【日志内容】:" + logMsg.Message);
  77. if (logMsg.Exception != null)
  78. {
  79. stringBuilder.AppendLine("【异常信息】:" + logMsg.Exception);
  80. }
  81. return stringBuilder.ToString();
  82. };
  83. }
  84. }
  85. }