add minimum log level setting (#877)

This commit is contained in:
Jason Dove
2022-06-27 10:29:04 -05:00
committed by GitHub
parent ae64ca4a93
commit 5ed0184bca
12 changed files with 241 additions and 73 deletions
@@ -0,0 +1,31 @@
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Repositories;
using Serilog.Core;
using Serilog.Events;
namespace ErsatzTV.Services.RunOnce;
public class LoadLoggingLevelService : IHostedService
{
private readonly IServiceScopeFactory _serviceScopeFactory;
public LoadLoggingLevelService(IServiceScopeFactory serviceScopeFactory) =>
_serviceScopeFactory = serviceScopeFactory;
public async Task StartAsync(CancellationToken cancellationToken)
{
using IServiceScope scope = _serviceScopeFactory.CreateScope();
IConfigElementRepository configElementRepository =
scope.ServiceProvider.GetRequiredService<IConfigElementRepository>();
Option<LogEventLevel> maybeLogLevel =
await configElementRepository.GetValue<LogEventLevel>(ConfigElementKey.MinimumLogLevel);
foreach (LogEventLevel logLevel in maybeLogLevel)
{
LoggingLevelSwitch loggingLevelSwitch = scope.ServiceProvider.GetRequiredService<LoggingLevelSwitch>();
loggingLevelSwitch.MinimumLevel = logLevel;
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}