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

88 line
3.5 KiB

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