@ -1,19 +1,20 @@
using Common.Logging ;
using Serilog ;
using Serilog ;
using Serilog.Core ;
using Serilog.Core ;
using Serilog.Events ;
using Serilog.Events ;
namespace Common.Logger ;
// alias to prevent conflict own Log method
using SerilogLoggingInstance = Serilog . Log ;
namespace Common.Logging ;
/// <summary>
/// <summary>
/// Implements a Logger that uses the Serilog package to create log entries and distributes them into different sinks.
/// Implements a Logger that uses the Serilog package to create log entries and distributes them into different sinks.
/// Implemented Sinks: File (rolling file), Debug (<see cref="System.Diagnostics.Debug"/>).
/// Implemented Sinks: File (rolling file), Debug (<see cref="System.Diagnostics.Debug"/>).
/// </summary>
/// </summary>
public class SerilogLogger : Logging . ILogger
public class SerilogLogger : ILogger
{
{
private readonly LoggingLevelSwitch loggingLevelSwitch ;
private readonly LoggingLevelSwitch loggingLevelSwitch ;
public event EventHandler < NewLogEntryEventArgs > ? NewLogEntry ;
public event EventHandler < NewLogEntryEventArgs > ? NewLogEntry ;
public LogLevel CurrentMinimumLogLevel { get ; private set ; } = LogLevel . Undefined ;
public LogLevel CurrentMinimumLogLevel { get ; private set ; } = LogLevel . Undefined ;
@ -38,7 +39,7 @@ public class SerilogLogger : Logging.ILogger
// create logger instance
// create logger instance
if ( logToDebug )
if ( logToDebug )
{
{
Log . Logger = new LoggerConfiguration ( )
Serilog LoggingInstance . Logger = new LoggerConfiguration ( )
. WriteTo . Debug ( )
. WriteTo . Debug ( )
. WriteTo . File ( logFilePath ,
. WriteTo . File ( logFilePath ,
rollingInterval : RollingInterval . Day ,
rollingInterval : RollingInterval . Day ,
@ -48,7 +49,7 @@ public class SerilogLogger : Logging.ILogger
}
}
else
else
{
{
Log . Logger = new LoggerConfiguration ( )
Serilog LoggingInstance . Logger = new LoggerConfiguration ( )
. WriteTo . File ( logFilePath ,
. WriteTo . File ( logFilePath ,
rollingInterval : RollingInterval . Day ,
rollingInterval : RollingInterval . Day ,
levelSwitch : loggingLevelSwitch ,
levelSwitch : loggingLevelSwitch ,
@ -87,10 +88,12 @@ public class SerilogLogger : Logging.ILogger
public void StopLogging ( )
public void StopLogging ( )
{
{
Log . CloseAndFlush ( ) ;
Serilog LoggingInstance . CloseAndFlush ( ) ;
}
}
private void CreateLogEntry ( LogEntry logEntry )
#region Logging methods
public void Log ( LogEntry logEntry )
{
{
var serilogEventLevel = this . ConvertGenericToSerilogLogLevel ( logEntry . LogLevel ) ;
var serilogEventLevel = this . ConvertGenericToSerilogLogLevel ( logEntry . LogLevel ) ;
@ -98,31 +101,31 @@ public class SerilogLogger : Logging.ILogger
switch ( serilogEventLevel )
switch ( serilogEventLevel )
{
{
case LogEventLevel . Verbose :
case LogEventLevel . Verbose :
Log . Verbose ( logEntry . ToString ( ) ) ;
Serilog LoggingInstance . Verbose ( logEntry . ToString ( ) ) ;
break ;
break ;
case LogEventLevel . Debug :
case LogEventLevel . Debug :
Log . Debug ( logEntry . ToString ( ) ) ;
Serilog LoggingInstance . Debug ( logEntry . ToString ( ) ) ;
break ;
break ;
case LogEventLevel . Information :
case LogEventLevel . Information :
Log . Information ( logEntry . ToString ( ) ) ;
Serilog LoggingInstance . Information ( logEntry . ToString ( ) ) ;
break ;
break ;
case LogEventLevel . Warning :
case LogEventLevel . Warning :
Log . Warning ( logEntry . ToString ( ) ) ;
Serilog LoggingInstance . Warning ( logEntry . ToString ( ) ) ;
break ;
break ;
case LogEventLevel . Error :
case LogEventLevel . Error :
Log . Error ( logEntry . ToString ( ) ) ;
Serilog LoggingInstance . Error ( logEntry . ToString ( ) ) ;
break ;
break ;
case LogEventLevel . Fatal :
case LogEventLevel . Fatal :
Log . Error ( logEntry . ToString ( ) ) ;
Serilog LoggingInstance . Error ( logEntry . ToString ( ) ) ;
break ;
break ;
default :
default :
throw new NotImplementedException ( $"'{nameof(Create LogEntry )}()' does not contain an implementation for {nameof(LogEventLevel)} {serilogEventLevel}." ) ;
throw new NotImplementedException ( $"'{nameof(Log)}()' does not contain an implementation for {nameof(LogEventLevel)} {serilogEventLevel}." ) ;
}
}
// Raise event (only if the log is wanted due to the configured log level)
// Raise event (only if the log is wanted due to the configured log level)
@ -132,41 +135,39 @@ public class SerilogLogger : Logging.ILogger
}
}
}
}
#region Logging methods
public void LogTrace ( string message , string category = "" )
public void LogTrace ( string message , string category = "" )
{
{
this . Create LogEntry ( new LogEntry { Category = category , LogLevel = LogLevel . Trace , Message = message } ) ;
this . Log ( new LogEntry { Category = category , LogLevel = LogLevel . Trace , Message = message } ) ;
}
}
public void LogDebug ( string message , string category = "" )
public void LogDebug ( string message , string category = "" )
{
{
this . Create LogEntry ( new LogEntry { Category = category , LogLevel = LogLevel . Debug , Message = message } ) ;
this . Log ( new LogEntry { Category = category , LogLevel = LogLevel . Debug , Message = message } ) ;
}
}
public void LogInfo ( string message , string category = "" )
public void LogInfo ( string message , string category = "" )
{
{
this . Create LogEntry ( new LogEntry { Category = category , LogLevel = LogLevel . Info , Message = message } ) ;
this . Log ( new LogEntry { Category = category , LogLevel = LogLevel . Info , Message = message } ) ;
}
}
public void LogWarn ( string message , string category = "" )
public void LogWarn ( string message , string category = "" )
{
{
this . Create LogEntry ( new LogEntry { Category = category , LogLevel = LogLevel . Warn , Message = message } ) ;
this . Log ( new LogEntry { Category = category , LogLevel = LogLevel . Warn , Message = message } ) ;
}
}
public void LogError ( string message , string category = "" )
public void LogError ( string message , string category = "" )
{
{
this . Create LogEntry ( new LogEntry { Category = category , LogLevel = LogLevel . Error , Message = message } ) ;
this . Log ( new LogEntry { Category = category , LogLevel = LogLevel . Error , Message = message } ) ;
}
}
public void LogException ( Exception exception , string category = "" )
public void LogException ( Exception exception , string category = "" )
{
{
this . Create LogEntry ( new LogEntry { Category = category , LogLevel = LogLevel . Error , Exception = exception } ) ;
this . Log ( new LogEntry { Category = category , LogLevel = LogLevel . Error , Exception = exception } ) ;
}
}
public void LogException ( Exception exception , string message , string category = "" )
public void LogException ( Exception exception , string message , string category = "" )
{
{
this . Create LogEntry (
this . Log (
new LogEntry
new LogEntry
{
{
Category = category ,
Category = category ,