forked from sim0n00ps/OF-DL
Switched to custom logging service using Seq as well as file.
This commit is contained in:
parent
41eea18d99
commit
b9b6beffa4
@ -19,6 +19,7 @@
|
||||
<PackageReference Include="Serilog" Version="4.3.1"/>
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1"/>
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0"/>
|
||||
<PackageReference Include="Serilog.Sinks.Seq" Version="7.0.0"/>
|
||||
<PackageReference Include="System.Reactive" Version="6.1.0"/>
|
||||
<PackageReference Include="xFFmpeg.NET" Version="7.2.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
50
OF DL.Core/Services/SeqLoggingService.cs
Normal file
50
OF DL.Core/Services/SeqLoggingService.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
@ -84,7 +84,7 @@ public class Program(IServiceProvider serviceProvider)
|
||||
{
|
||||
// Set up dependency injection with LoggingService and ConfigService
|
||||
ServiceCollection services = new();
|
||||
services.AddSingleton<ILoggingService, LoggingService>();
|
||||
services.AddSingleton<ILoggingService, SeqLoggingService>();
|
||||
services.AddSingleton<IConfigService, ConfigService>();
|
||||
services.AddSingleton<ExitHelper>();
|
||||
ServiceProvider tempServiceProvider = services.BuildServiceProvider();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user