From d40c7c63a8070fbfc2bfc7dc7b289aa04805b0ec Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Sat, 8 Apr 2023 14:02:42 +0200 Subject: [PATCH] implemented interface for a generic logger --- Common/Logging/ILogger.cs | 90 +++++++++++++++++++++ Common/Logging/LogEntry.cs | 103 +++++++++++++++++++++++++ Common/Logging/LogLevel.cs | 14 ++++ Common/Logging/NewLogEntryEventArgs.cs | 14 ++++ 4 files changed, 221 insertions(+) create mode 100644 Common/Logging/ILogger.cs create mode 100644 Common/Logging/LogEntry.cs create mode 100644 Common/Logging/LogLevel.cs create mode 100644 Common/Logging/NewLogEntryEventArgs.cs diff --git a/Common/Logging/ILogger.cs b/Common/Logging/ILogger.cs new file mode 100644 index 0000000..766525f --- /dev/null +++ b/Common/Logging/ILogger.cs @@ -0,0 +1,90 @@ +namespace Common.Logging; + +/// +/// Generalized interface for a logger. +/// Contains Initialization and Stopping methods, an event for when a new log entry is entered. +/// Contains various methods to create a log entry. +/// +public interface ILogger +{ + /// + /// Event that is thrown whenever a new log entry was entered with any Log method. + /// + event EventHandler NewLogEntry; + + /// + /// Initialize the Logger once before using it. + /// Sets the minimum to . Therefore all log entries will be logged. + /// + void Initialize(); + + /// + /// Initialize the Logger once before using it. + /// + /// log entries with a lower level than this will not be logged + void Initialize(LogLevel minimumLogLevel); + + /// + /// Empties queue and closes logging stream. + /// + void StopLogging(); + + /// + /// Changes minimum to the given level. + /// + /// log entries with a lower level than this will not be logged anymore + void SetMinimumLogLevel(LogLevel newMinimumLogLevel); + + #region Logging methods + /// + /// Create a Log entry with the level . + /// + /// Category of the log entry. Example: Location in Software + /// Message to describe what to log. + void LogTrace(string message, string category); + + /// + /// Create a Log entry with the level . + /// + /// Category of the log entry. Example: Location in Software + /// Message to describe what to log. + void LogDebug(string message, string category); + + /// + /// Create a Log entry with the level . + /// + /// Category of the log entry. Example: Location in Software + /// Message to describe what to log. + void LogInfo(string message, string category); + + /// + /// Create a Log entry with the level . + /// + /// Category of the log entry. Example: Location in Software + /// Message to describe what to log. + void LogWarn(string message, string category); + + /// + /// Create a Log entry with the level . + /// + /// Category of the log entry. Example: Location in Software + /// Message to describe what to log. + void LogError(string message, string category); + + /// + /// Create a Log entry with a assigned. No message provided. + /// + /// Exception associated with the log entry. + /// Category of the log entry. Example: Location in Software + void LogException(Exception exception, string category); + + /// + /// Create a Log entry with a assigned. + /// + /// Exception associated with the log entry. + /// Message to describe what to log. + /// Category of the log entry. Example: Location in Software + void LogException(Exception exception, string message, string category); + + #endregion +} diff --git a/Common/Logging/LogEntry.cs b/Common/Logging/LogEntry.cs new file mode 100644 index 0000000..a79e795 --- /dev/null +++ b/Common/Logging/LogEntry.cs @@ -0,0 +1,103 @@ +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(); + } +} diff --git a/Common/Logging/LogLevel.cs b/Common/Logging/LogLevel.cs new file mode 100644 index 0000000..bd02a1c --- /dev/null +++ b/Common/Logging/LogLevel.cs @@ -0,0 +1,14 @@ +namespace Common.Logging; + +/// +/// Level of a log entry. +/// +public enum LogLevel +{ + Undefined = 0, + Error = 1, + Warn = 2, + Info = 3, + Debug = 4, + Trace = 5 +} diff --git a/Common/Logging/NewLogEntryEventArgs.cs b/Common/Logging/NewLogEntryEventArgs.cs new file mode 100644 index 0000000..82a9f55 --- /dev/null +++ b/Common/Logging/NewLogEntryEventArgs.cs @@ -0,0 +1,14 @@ +namespace Common.Logging; + +/// +/// EventArgs for when a new log entry is present. +/// +public class NewLogEntryEventArgs : EventArgs +{ + public LogEntry LogEntry { get; } + + public NewLogEntryEventArgs(LogEntry logEntry) + { + this.LogEntry = logEntry; + } +}