Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MultiTerm/Common/Logging/LogEntry.cs

103 lines
3.8 KiB

using System.Text;
namespace Common.Logging;
/// <summary>
/// A generalized class that represents a single log entry.
/// </summary>
public class LogEntry
{
/// <summary>
/// Category of the <see cref="LogEntry"/>.
/// Example: Location in Software
/// </summary>
public string Category { get; set; }
/// <summary>
/// Message to describe what to log.
/// </summary>
public string Message { get; set; }
/// <summary>
/// Time when the <see cref="LogEntry"/> was created.
/// </summary>
public DateTime Time { get; }
/// <summary>
/// Time when the <see cref="LogEntry"/> was created as a formatted string.
/// </summary>
public string TimeString => this.Time.ToString("dd.MM.yyyy HH:mm:ss.fff");
/// <summary>
/// <see cref="System.Exception"/> associated with the <see cref="LogEntry"/>, may be null.
/// </summary>
public Exception? Exception { get; set; }
/// <summary>
/// <see cref="Logging.LogLevel"/> associated with the <see cref="LogEntry"/>.
/// </summary>
public LogLevel LogLevel { get; set; }
/// <summary>
/// Creates an empty <see cref="LogEntry"/> with no <see cref="Category"/> and <see cref="Message"/>.
/// </summary>
public LogEntry()
{
this.Time = DateTime.Now;
this.Category = string.Empty;
this.Message = string.Empty;
}
/// <summary>
/// Creates a <see cref="LogEntry"/> with the information given as parameters.
/// </summary>
/// <param name="level"><see cref="Logging.LogLevel"/> associated with the <see cref="LogEntry"/></param>
/// <param name="category">Category of the <see cref="LogEntry"/>. Example: Location in Software</param>
/// <param name="message">Message to describe what to log.</param>
/// <param name="ex"><see cref="System.Exception"/> associated with the <see cref="LogEntry"/>, may be null.</param>
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;
}
/// <summary>
/// Creates a <see cref="LogEntry"/> with the information given as parameters.
/// Sets the category of the <see cref="LogEntry"/> to an empty string.
/// No <see cref="System.Exception"/> is assigned to the <see cref="LogEntry"/>.
/// </summary>
/// <param name="level"><see cref="Logging.LogLevel"/> associated with the <see cref="LogEntry"/></param>
/// <param name="message">Message to describe what to log.</param>
public LogEntry(LogLevel level, string message)
: this(level, string.Empty, message, null) { }
/// <summary>
/// Creates a <see cref="LogEntry"/> with the information given as parameters.
/// No <see cref="System.Exception"/> is assigned to the <see cref="LogEntry"/>.
/// </summary>
/// <param name="level"><see cref="Logging.LogLevel"/> associated with the <see cref="LogEntry"/></param>
/// <param name="category">Category of the <see cref="LogEntry"/>. Example: Location in Software</param>
/// <param name="message">Message to describe what to log.</param>
public LogEntry(LogLevel level, string category, string message)
: this(level, category, message, null) { }
/// <summary>
/// Builds the string that is to log for this entry.
/// </summary>
/// <returns>string with complete log entry</returns>
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();
}
}