using Serilog.Core; using Serilog.Events; namespace OF_DL.Services; public class CajetanLoggingService : ILoggingService { public CajetanLoggingService() { LevelSwitch = new LoggingLevelSwitch(); InitializeLoggerWithSeq(); } /// /// Gets the level switch that controls runtime logging verbosity. /// public LoggingLevelSwitch LevelSwitch { get; } /// /// Updates the minimum logging level at runtime. /// /// The new minimum log level. public void UpdateLoggingLevel(LoggingLevel newLevel) { LevelSwitch.MinimumLevel = (LogEventLevel)newLevel; Log.Debug("Logging level updated to: {LoggingLevel}", newLevel); } /// /// Returns the current minimum logging level. /// 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"); } }