| 123456789101112131415161718192021222324252627282930313233343536373839 |
- namespace YZWater.Core.Models;
- /// <summary>
- /// 日志条目模型
- /// </summary>
- public class LogEntry
- {
- /// <summary>时间戳</summary>
- public DateTime Timestamp { get; set; } = DateTime.Now;
- /// <summary>日志级别(Information, Warning, Error, Debug)</summary>
- public string Level { get; set; } = "Information";
- /// <summary>日志消息</summary>
- public string Message { get; set; } = string.Empty;
- /// <summary>异常信息(可选)</summary>
- public string? Exception { get; set; }
- /// <summary>来源(可选)</summary>
- public string? Source { get; set; }
- /// <summary>
- /// 从 Serilog 的 LogEvent 创建 LogEntry
- /// </summary>
- public static LogEntry FromSerilog(Serilog.Events.LogEvent logEvent)
- {
- return new LogEntry
- {
- Timestamp = logEvent.Timestamp.LocalDateTime,
- Level = logEvent.Level.ToString(),
- Message = logEvent.RenderMessage(),
- Exception = logEvent.Exception?.ToString(),
- Source = logEvent.Properties.TryGetValue("SourceContext", out var sourceContext)
- ? sourceContext.ToString().Trim('"')
- : null
- };
- }
- }
|