LogEntry.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. namespace YZWater.Core.Models;
  2. /// <summary>
  3. /// 日志条目模型
  4. /// </summary>
  5. public class LogEntry
  6. {
  7. /// <summary>时间戳</summary>
  8. public DateTime Timestamp { get; set; } = DateTime.Now;
  9. /// <summary>日志级别(Information, Warning, Error, Debug)</summary>
  10. public string Level { get; set; } = "Information";
  11. /// <summary>日志消息</summary>
  12. public string Message { get; set; } = string.Empty;
  13. /// <summary>异常信息(可选)</summary>
  14. public string? Exception { get; set; }
  15. /// <summary>来源(可选)</summary>
  16. public string? Source { get; set; }
  17. /// <summary>
  18. /// 从 Serilog 的 LogEvent 创建 LogEntry
  19. /// </summary>
  20. public static LogEntry FromSerilog(Serilog.Events.LogEvent logEvent)
  21. {
  22. return new LogEntry
  23. {
  24. Timestamp = logEvent.Timestamp.LocalDateTime,
  25. Level = logEvent.Level.ToString(),
  26. Message = logEvent.RenderMessage(),
  27. Exception = logEvent.Exception?.ToString(),
  28. Source = logEvent.Properties.TryGetValue("SourceContext", out var sourceContext)
  29. ? sourceContext.ToString().Trim('"')
  30. : null
  31. };
  32. }
  33. }