|
| 1 | +using MongoDB.Bson; |
| 2 | +using MongoDB.Bson.Serialization.Attributes; |
| 3 | + |
| 4 | +namespace TelegramDownloader.Models |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Represents a log entry stored in MongoDB |
| 8 | + /// </summary> |
| 9 | + [BsonIgnoreExtraElements] |
| 10 | + public class LogEntry |
| 11 | + { |
| 12 | + [BsonId] |
| 13 | + [BsonRepresentation(BsonType.ObjectId)] |
| 14 | + public string? Id { get; set; } |
| 15 | + |
| 16 | + [BsonElement("UtcTimeStamp")] |
| 17 | + [BsonDateTimeOptions(Kind = DateTimeKind.Utc)] |
| 18 | + public DateTime Timestamp { get; set; } |
| 19 | + |
| 20 | + [BsonElement("Level")] |
| 21 | + public string Level { get; set; } = "Information"; |
| 22 | + |
| 23 | + [BsonElement("RenderedMessage")] |
| 24 | + public string Message { get; set; } = string.Empty; |
| 25 | + |
| 26 | + [BsonElement("Exception")] |
| 27 | + public BsonValue? ExceptionRaw { get; set; } |
| 28 | + |
| 29 | + [BsonElement("Properties")] |
| 30 | + public BsonDocument? Properties { get; set; } |
| 31 | + |
| 32 | + // Helper properties for display |
| 33 | + [BsonIgnore] |
| 34 | + public string? Exception => ExceptionRaw?.IsBsonDocument == true |
| 35 | + ? ExceptionRaw.AsBsonDocument.ToString() |
| 36 | + : ExceptionRaw?.ToString(); |
| 37 | + |
| 38 | + [BsonIgnore] |
| 39 | + public string Logger => GetPropertyValue("SourceContext") ?? "Unknown"; |
| 40 | + |
| 41 | + [BsonIgnore] |
| 42 | + public string Version => GetPropertyValue("AppVersion") ?? "Unknown"; |
| 43 | + |
| 44 | + [BsonIgnore] |
| 45 | + public string ShortLogger |
| 46 | + { |
| 47 | + get |
| 48 | + { |
| 49 | + var logger = Logger; |
| 50 | + if (string.IsNullOrEmpty(logger)) return "Unknown"; |
| 51 | + var parts = logger.Split('.'); |
| 52 | + return parts.Length > 0 ? parts[^1] : logger; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private string? GetPropertyValue(string key) |
| 57 | + { |
| 58 | + if (Properties == null) return null; |
| 59 | + if (Properties.TryGetValue(key, out var value)) |
| 60 | + { |
| 61 | + return value?.ToString(); |
| 62 | + } |
| 63 | + return null; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Request parameters for log queries |
| 69 | + /// </summary> |
| 70 | + public class LogQueryRequest |
| 71 | + { |
| 72 | + public int Page { get; set; } = 1; |
| 73 | + public int PageSize { get; set; } = 50; |
| 74 | + public DateTime? FromDate { get; set; } |
| 75 | + public DateTime? ToDate { get; set; } |
| 76 | + public string? Level { get; set; } |
| 77 | + public string? Logger { get; set; } |
| 78 | + public string? Version { get; set; } |
| 79 | + public string? SearchText { get; set; } |
| 80 | + public bool DescendingOrder { get; set; } = true; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Result of a log query with pagination info |
| 85 | + /// </summary> |
| 86 | + public class LogQueryResult |
| 87 | + { |
| 88 | + public List<LogEntry> Logs { get; set; } = new(); |
| 89 | + public int TotalCount { get; set; } |
| 90 | + public int Page { get; set; } |
| 91 | + public int PageSize { get; set; } |
| 92 | + public int TotalPages => PageSize > 0 ? (int)Math.Ceiling(TotalCount / (double)PageSize) : 0; |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Statistics about log entries |
| 97 | + /// </summary> |
| 98 | + public class LogStats |
| 99 | + { |
| 100 | + public long TotalLogs { get; set; } |
| 101 | + public long ErrorCount { get; set; } |
| 102 | + public long WarningCount { get; set; } |
| 103 | + public long InfoCount { get; set; } |
| 104 | + public long DebugCount { get; set; } |
| 105 | + public DateTime? OldestLog { get; set; } |
| 106 | + public DateTime? NewestLog { get; set; } |
| 107 | + } |
| 108 | +} |
0 commit comments