LoggerBuilder.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // <author></author>
  2. // <date></date>
  3. // <description></description>
  4. using ERP.Framework.Config;
  5. using Serilog;
  6. namespace ERP.Framework.Logger
  7. {
  8. public static class LoggerBuilder
  9. {
  10. public static ILogger CreateLogger(LogConfig logConfig)
  11. {
  12. // 扩展增加Elk 或 MongoDB等
  13. //if (logConfig.LogType == Enum.LogTypeEnum.Els)
  14. //{
  15. // return ...
  16. //}
  17. return AddLocalLog(logConfig);
  18. }
  19. private static ILogger AddLocalLog(LogConfig logConfig)
  20. {
  21. var outputTemplete = "[{Timestep:yyyy-MM-dd HH:mm:ss.fff}] {Level} ({SourceContext}) {Message} {NewLine} {Exception}";
  22. var logConfiguration = new LoggerConfiguration()
  23. .MinimumLevel.Debug()
  24. .WriteTo.File(logConfig.FilePath,
  25. rollingInterval: RollingInterval.Day,// 文件类别(分支,小时,天)
  26. outputTemplate: outputTemplete, // 日志模板
  27. retainedFileCountLimit: 31, // 日志文件最大个数
  28. retainedFileTimeLimit: TimeSpan.FromDays(7), // 日志保存时间
  29. rollOnFileSizeLimit: true, // 是否限制单个文件最大大小
  30. fileSizeLimitBytes: 52428800 // 单个文件最大大小
  31. );
  32. return logConfiguration.CreateLogger();
  33. }
  34. }
  35. }