implemented interface for a generic logger

master
Jonas Arnold 3 years ago
parent c55411d6c9
commit d40c7c63a8
  1. 90
      Common/Logging/ILogger.cs
  2. 103
      Common/Logging/LogEntry.cs
  3. 14
      Common/Logging/LogLevel.cs
  4. 14
      Common/Logging/NewLogEntryEventArgs.cs

@ -0,0 +1,90 @@
namespace Common.Logging;
/// <summary>
/// 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.
/// </summary>
public interface ILogger
{
/// <summary>
/// Event that is thrown whenever a new log entry was entered with any Log method.
/// </summary>
event EventHandler<NewLogEntryEventArgs> NewLogEntry;
/// <summary>
/// Initialize the Logger once before using it.
/// Sets the minimum<see cref="Logging.LogLevel"/> to <see cref="Logging.LogLevel.Trace"/>. Therefore all log entries will be logged.
/// </summary>
void Initialize();
/// <summary>
/// Initialize the Logger once before using it.
/// </summary>
/// <param name="minimumLogLevel">log entries with a lower level than this will not be logged</param>
void Initialize(LogLevel minimumLogLevel);
/// <summary>
/// Empties queue and closes logging stream.
/// </summary>
void StopLogging();
/// <summary>
/// Changes minimum<see cref="Logging.LogLevel"/> to the given level.
/// </summary>
/// <param name="newMinimumLogLevel">log entries with a lower level than this will not be logged anymore</param>
void SetMinimumLogLevel(LogLevel newMinimumLogLevel);
#region Logging methods
/// <summary>
/// Create a Log entry with the level <see cref="Logging.LogLevel.Trace"/>.
/// </summary>
/// <param name="category">Category of the log entry. Example: Location in Software</param>
/// <param name="message">Message to describe what to log.</param>
void LogTrace(string message, string category);
/// <summary>
/// Create a Log entry with the level <see cref="Logging.LogLevel.Debug"/>.
/// </summary>
/// <param name="category">Category of the log entry. Example: Location in Software</param>
/// <param name="message">Message to describe what to log.</param>
void LogDebug(string message, string category);
/// <summary>
/// Create a Log entry with the level <see cref="Logging.LogLevel.Info"/>.
/// </summary>
/// <param name="category">Category of the log entry. Example: Location in Software</param>
/// <param name="message">Message to describe what to log.</param>
void LogInfo(string message, string category);
/// <summary>
/// Create a Log entry with the level <see cref="Logging.LogLevel.Warn"/>.
/// </summary>
/// <param name="category">Category of the log entry. Example: Location in Software</param>
/// <param name="message">Message to describe what to log.</param>
void LogWarn(string message, string category);
/// <summary>
/// Create a Log entry with the level <see cref="Logging.LogLevel.Error"/>.
/// </summary>
/// <param name="category">Category of the log entry. Example: Location in Software</param>
/// <param name="message">Message to describe what to log.</param>
void LogError(string message, string category);
/// <summary>
/// Create a Log entry with a <see cref="System.Exception"/> assigned. No message provided.
/// </summary>
/// <param name="exception">Exception associated with the log entry.</param>
/// <param name="category">Category of the log entry. Example: Location in Software</param>
void LogException(Exception exception, string category);
/// <summary>
/// Create a Log entry with a <see cref="System.Exception"/> assigned.
/// </summary>
/// <param name="exception">Exception associated with the log entry.</param>
/// <param name="message">Message to describe what to log.</param>
/// <param name="category">Category of the log entry. Example: Location in Software</param>
void LogException(Exception exception, string message, string category);
#endregion
}

@ -0,0 +1,103 @@
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();
}
}

@ -0,0 +1,14 @@
namespace Common.Logging;
/// <summary>
/// Level of a log entry.
/// </summary>
public enum LogLevel
{
Undefined = 0,
Error = 1,
Warn = 2,
Info = 3,
Debug = 4,
Trace = 5
}

@ -0,0 +1,14 @@
namespace Common.Logging;
/// <summary>
/// EventArgs for when a new log entry is present.
/// </summary>
public class NewLogEntryEventArgs : EventArgs
{
public LogEntry LogEntry { get; }
public NewLogEntryEventArgs(LogEntry logEntry)
{
this.LogEntry = logEntry;
}
}
Loading…
Cancel
Save