forked from sim0n00ps/OF-DL
55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
using Serilog.Core;
|
|
using Serilog.Events;
|
|
|
|
namespace OF_DL.Services;
|
|
|
|
public class CajetanLoggingService : ILoggingService
|
|
{
|
|
public CajetanLoggingService()
|
|
{
|
|
LevelSwitch = new LoggingLevelSwitch();
|
|
InitializeLoggerWithSeq();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the level switch that controls runtime logging verbosity.
|
|
/// </summary>
|
|
public LoggingLevelSwitch LevelSwitch { get; }
|
|
|
|
/// <summary>
|
|
/// Updates the minimum logging level at runtime.
|
|
/// </summary>
|
|
/// <param name="newLevel">The new minimum log level.</param>
|
|
public void UpdateLoggingLevel(LoggingLevel newLevel)
|
|
{
|
|
LevelSwitch.MinimumLevel = (LogEventLevel)newLevel;
|
|
Log.Debug("Logging level updated to: {LoggingLevel}", newLevel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the current minimum logging level.
|
|
/// </summary>
|
|
public LoggingLevel GetCurrentLoggingLevel() => (LoggingLevel)LevelSwitch.MinimumLevel;
|
|
|
|
private void InitializeLoggerWithSeq()
|
|
{
|
|
LevelSwitch.MinimumLevel = LogEventLevel.Warning;
|
|
|
|
LoggerConfiguration loggerConfig = new LoggerConfiguration()
|
|
.Enrich.FromLogContext()
|
|
.Enrich.WithProperty("Application", "OF_DL")
|
|
.Enrich.WithProperty("StartTime", $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} ")
|
|
.Enrich.WithProperty("MachineName", Environment.MachineName)
|
|
.MinimumLevel.Verbose()
|
|
.WriteTo.File("logs/OFDL.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Error, levelSwitch: LevelSwitch)
|
|
.WriteTo.Seq("https://seq.cajetan.dk", controlLevelSwitch: LevelSwitch);
|
|
|
|
if (System.Diagnostics.Debugger.IsAttached)
|
|
loggerConfig.WriteTo.Debug(restrictedToMinimumLevel: LogEventLevel.Debug);
|
|
|
|
Log.Logger = loggerConfig.CreateLogger();
|
|
|
|
Log.Debug("Logging service initialized");
|
|
}
|
|
}
|