using System.Text; namespace Common.Logging; /// /// A generalized class that represents a single log entry. /// public class LogEntry { /// /// Category of the . /// Example: Location in Software /// public string Category { get; set; } /// /// Message to describe what to log. /// public string Message { get; set; } /// /// Time when the was created. /// public DateTime Time { get; } /// /// Time when the was created as a formatted string. /// public string TimeString => this.Time.ToString("dd.MM.yyyy HH:mm:ss.fff"); /// /// associated with the , may be null. /// public Exception? Exception { get; set; } /// /// associated with the . /// public LogLevel LogLevel { get; set; } /// /// Creates an empty with no and . /// public LogEntry() { this.Time = DateTime.Now; this.Category = string.Empty; this.Message = string.Empty; } /// /// Creates a with the information given as parameters. /// /// associated with the /// Category of the . Example: Location in Software /// Message to describe what to log. /// associated with the , may be null. public LogEntry(LogLevel level, string category, string message, Exception? ex) { this.Time = DateTime.Now; this.LogLevel = level; this.Category = category; this.Message = message; this.Exception = ex; } /// /// Creates a with the information given as parameters. /// Sets the category of the to an empty string. /// No is assigned to the . /// /// associated with the /// Message to describe what to log. public LogEntry(LogLevel level, string message) : this(level, string.Empty, message, null) { } /// /// Creates a with the information given as parameters. /// No is assigned to the . /// /// associated with the /// Category of the . Example: Location in Software /// Message to describe what to log. public LogEntry(LogLevel level, string category, string message) : this(level, category, message, null) { } /// /// Builds the string that is to log for this entry. /// /// string with complete log entry public override string ToString() { StringBuilder sb = new(); sb.AppendLine($"{this.TimeString} {this.LogLevel.ToString().ToUpper()} {this.Category}: {this.Message}"); if (this.Exception != null) { sb.AppendLine($"Exception: {this.Exception}"); } return sb.ToString(); } }