From 7adf784e9f888e95ff91b5b0611554af24960d01 Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Tue, 17 Feb 2026 22:51:00 +0100 Subject: [PATCH] Added to custom logging service using Seq. --- Cajetan.OF-DL/Cajetan.OF-DL.csproj | 1 + Cajetan.OF-DL/Services/SeqLoggingService.cs | 50 +++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Cajetan.OF-DL/Services/SeqLoggingService.cs diff --git a/Cajetan.OF-DL/Cajetan.OF-DL.csproj b/Cajetan.OF-DL/Cajetan.OF-DL.csproj index b63f27b..0e0fd7d 100644 --- a/Cajetan.OF-DL/Cajetan.OF-DL.csproj +++ b/Cajetan.OF-DL/Cajetan.OF-DL.csproj @@ -30,6 +30,7 @@ + diff --git a/Cajetan.OF-DL/Services/SeqLoggingService.cs b/Cajetan.OF-DL/Services/SeqLoggingService.cs new file mode 100644 index 0000000..6efcea4 --- /dev/null +++ b/Cajetan.OF-DL/Services/SeqLoggingService.cs @@ -0,0 +1,50 @@ +using OF_DL.Enumerations; +using Serilog; +using Serilog.Core; +using Serilog.Events; + +namespace OF_DL.Services; + +public class SeqLoggingService : ILoggingService +{ + public SeqLoggingService() + { + 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() + { + Log.Logger = 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.ControlledBy(LevelSwitch) + .WriteTo.File("logs/OFDL.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Error) + .WriteTo.Seq("https://seq.cajetan.dk") + .CreateLogger(); + + Log.Debug("Logging service initialized"); + } +}