Added to custom logging service using Seq.

This commit is contained in:
Casper Sparre 2026-02-17 22:51:00 +01:00
parent 8f091d56ca
commit 7adf784e9f
2 changed files with 51 additions and 0 deletions

View File

@ -30,6 +30,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Serilog.Sinks.Seq" Version="7.0.1" />
<PackageReference Include="Akka" Version="1.5.60"/> <PackageReference Include="Akka" Version="1.5.60"/>
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1"/> <PackageReference Include="BouncyCastle.NetCore" Version="2.2.1"/>
<PackageReference Include="HtmlAgilityPack" Version="1.12.4"/> <PackageReference Include="HtmlAgilityPack" Version="1.12.4"/>

View File

@ -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();
}
/// <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()
{
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");
}
}