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
+4
View File
@@ -11,6 +11,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- In previous versions, some libraries would incorrectly display only one item
- Properly display old versions of renamed items in trash
### Added
- Add `Minimum Log Level` option to `Settings` page
- Other methods of configuring the log level will no longer work
## [0.6.2-beta] - 2022-06-18
### Fixed
- Fix content repeating for up to a minute near the top of every hour
@@ -0,0 +1,5 @@
using ErsatzTV.Core;
namespace ErsatzTV.Application.Configuration;
public record UpdateGeneralSettings(GeneralSettingsViewModel GeneralSettings) : IRequest<Either<BaseError, Unit>>;
@@ -0,0 +1,32 @@
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Repositories;
using Serilog.Core;
namespace ErsatzTV.Application.Configuration;
public class UpdateGeneralSettingsHandler : IRequestHandler<UpdateGeneralSettings, Either<BaseError, Unit>>
{
private readonly IConfigElementRepository _configElementRepository;
private readonly LoggingLevelSwitch _loggingLevelSwitch;
public UpdateGeneralSettingsHandler(
LoggingLevelSwitch loggingLevelSwitch,
IConfigElementRepository configElementRepository)
{
_loggingLevelSwitch = loggingLevelSwitch;
_configElementRepository = configElementRepository;
}
public async Task<Either<BaseError, Unit>> Handle(
UpdateGeneralSettings request,
CancellationToken cancellationToken) => await ApplyUpdate(request.GeneralSettings);
private async Task<Unit> ApplyUpdate(GeneralSettingsViewModel generalSettings)
{
await _configElementRepository.Upsert(ConfigElementKey.MinimumLogLevel, generalSettings.MinimumLogLevel);
_loggingLevelSwitch.MinimumLevel = generalSettings.MinimumLogLevel;
return Unit.Default;
}
}
@@ -0,0 +1,8 @@
using Serilog.Events;
namespace ErsatzTV.Application.Configuration;
public class GeneralSettingsViewModel
{
public LogEventLevel MinimumLogLevel { get; set; }
}
@@ -0,0 +1,3 @@
namespace ErsatzTV.Application.Configuration;
public record GetGeneralSettings : IRequest<GeneralSettingsViewModel>;
@@ -0,0 +1,24 @@
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Repositories;
using Serilog.Events;
namespace ErsatzTV.Application.Configuration;
public class GetGeneralSettingsHandler : IRequestHandler<GetGeneralSettings, GeneralSettingsViewModel>
{
private readonly IConfigElementRepository _configElementRepository;
public GetGeneralSettingsHandler(IConfigElementRepository configElementRepository) =>
_configElementRepository = configElementRepository;
public async Task<GeneralSettingsViewModel> Handle(GetGeneralSettings request, CancellationToken cancellationToken)
{
Option<LogEventLevel> maybeLogLevel =
await _configElementRepository.GetValue<LogEventLevel>(ConfigElementKey.MinimumLogLevel);
return new GeneralSettingsViewModel
{
MinimumLogLevel = await maybeLogLevel.IfNoneAsync(LogEventLevel.Information)
};
}
}
+1
View File
@@ -6,6 +6,7 @@ public class ConfigElementKey
public string Key { get; }
public static ConfigElementKey MinimumLogLevel => new("log.minimum_level");
public static ConfigElementKey FFmpegPath => new("ffmpeg.ffmpeg_path");
public static ConfigElementKey FFprobePath => new("ffmpeg.ffprobe_path");
public static ConfigElementKey FFmpegDefaultProfileId => new("ffmpeg.default_profile_id");
@@ -14,7 +14,7 @@ public class ConfigElementRepository : IConfigElementRepository
public async Task<Unit> Upsert<T>(ConfigElementKey configElementKey, T value)
{
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
Option<ConfigElement> maybeElement = await dbContext.ConfigElements
.SelectOneAsync(c => c.Key, c => c.Key == configElementKey.Key);
@@ -42,7 +42,7 @@ public class ConfigElementRepository : IConfigElementRepository
public async Task<Option<ConfigElement>> Get(ConfigElementKey key)
{
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
return await dbContext.ConfigElements
.OrderBy(ce => ce.Key)
.SingleOrDefaultAsync(ce => ce.Key == key.Key)
@@ -50,18 +50,27 @@ public class ConfigElementRepository : IConfigElementRepository
}
public Task<Option<T>> GetValue<T>(ConfigElementKey key) =>
Get(key).MapT(ce => (T)Convert.ChangeType(ce.Value, typeof(T)));
Get(key).MapT(
ce =>
{
if (typeof(T).IsEnum)
{
return (T)Enum.Parse(typeof(T), ce.Value);
}
return (T)Convert.ChangeType(ce.Value, typeof(T));
});
public async Task Delete(ConfigElement configElement)
{
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
dbContext.ConfigElements.Remove(configElement);
await dbContext.SaveChangesAsync();
}
public async Task<Unit> Delete(ConfigElementKey configElementKey)
{
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
Option<ConfigElement> maybeExisting = await dbContext.ConfigElements
.SelectOneAsync(ce => ce.Key, ce => ce.Key == configElementKey.Key);
foreach (ConfigElement element in maybeExisting)
+45 -5
View File
@@ -6,6 +6,7 @@
@using ErsatzTV.Application.Watermarks
@using System.Globalization
@using ErsatzTV.Application.Configuration
@using Serilog.Events
@using ErsatzTV.Core.Domain.Filler
@implements IDisposable
@inject IMediator _mediator
@@ -13,7 +14,7 @@
@inject ILogger<Settings> _logger
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pt-8" Style="display: flex; flex-direction: row">
<div style="display: flex; flex-direction: row; flex-wrap: wrap">
<MudGrid>
<MudCard Class="mr-6 mb-6" Style="max-width: 400px">
<MudCardHeader>
<CardHeaderContent>
@@ -100,7 +101,31 @@
<MudButton Variant="Variant.Filled" Color="Color.Primary" Disabled="@(!_success)" OnClick="@(_ => SaveFFmpegSettings())">Save Settings</MudButton>
</MudCardActions>
</MudCard>
<MudCard Class="mr-6 mb-auto" Style="width: 350px">
<MudStack Class="mr-6">
<MudCard Class="mb-6" Style="width: 350px">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6">General Settings</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudForm>
<MudSelect Class="mt-3"
Label="Minimum Log Level"
@bind-Value="_generalSettings.MinimumLogLevel"
For="@(() => _generalSettings.MinimumLogLevel)">
<MudSelectItem Value="@LogEventLevel.Debug">Debug</MudSelectItem>
<MudSelectItem Value="@LogEventLevel.Information">Information</MudSelectItem>
<MudSelectItem Value="@LogEventLevel.Warning">Warning</MudSelectItem>
<MudSelectItem Value="@LogEventLevel.Error">Error</MudSelectItem>
</MudSelect>
</MudForm>
</MudCardContent>
<MudCardActions>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(_ => SaveGeneralSettings())">Save Settings</MudButton>
</MudCardActions>
</MudCard>
<MudCard Class="mb-6" Style="width: 350px">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6">HDHomeRun Settings</MudText>
@@ -115,7 +140,7 @@
<MudButton Variant="Variant.Filled" Color="Color.Primary" Disabled="@(!_hdhrSuccess)" OnClick="@(_ => SaveHDHRSettings())">Save Settings</MudButton>
</MudCardActions>
</MudCard>
<MudCard Class="mr-6" Style="width: 350px">
<MudCard Class="mb-6" Style="width: 350px">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6">Scanner Settings</MudText>
@@ -137,7 +162,7 @@
<MudButton Variant="Variant.Filled" Color="Color.Primary" Disabled="@(!_scannerSuccess)" OnClick="@(_ => SaveScannerSettings())">Save Settings</MudButton>
</MudCardActions>
</MudCard>
<MudCard Style="width: 350px">
<MudCard Class="mb-6" Style="width: 350px">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6">Playout Settings</MudText>
@@ -166,7 +191,8 @@
<MudButton Variant="Variant.Filled" Color="Color.Primary" Disabled="@(!_playoutSuccess)" OnClick="@(_ => SavePlayoutSettings())">Save Settings</MudButton>
</MudCardActions>
</MudCard>
</div>
</MudStack>
</MudGrid>
</MudContainer>
@code {
@@ -184,6 +210,7 @@
private int _tunerCount;
private int _libraryRefreshInterval;
private PlayoutSettingsViewModel _playoutSettings;
private GeneralSettingsViewModel _generalSettings;
public void Dispose()
{
@@ -207,6 +234,7 @@
_scannerSuccess = _libraryRefreshInterval > 0;
_playoutSettings = await _mediator.Send(new GetPlayoutSettings(), _cts.Token);
_playoutSuccess = _playoutSettings.DaysToBuild > 0;
_generalSettings = await _mediator.Send(new GetGeneralSettings(), _cts.Token);
}
private static string ValidatePathExists(string path) => !File.Exists(path) ? "Path does not exist" : null;
@@ -274,4 +302,16 @@
Right: _ => _snackbar.Add("Successfully saved playout settings", Severity.Success));
}
private async Task SaveGeneralSettings()
{
Either<BaseError, Unit> result = await _mediator.Send(new UpdateGeneralSettings(_generalSettings), _cts.Token);
result.Match(
Left: error =>
{
_snackbar.Add(error.Value, Severity.Error);
_logger.LogError("Unexpected error saving general settings: {Error}", error.Value);
},
Right: _ => _snackbar.Add("Successfully saved general settings", Severity.Success));
}
}
+10
View File
@@ -2,6 +2,8 @@ using System.Diagnostics;
using Destructurama;
using ErsatzTV.Core;
using Serilog;
using Serilog.Core;
using Serilog.Events;
namespace ErsatzTV;
@@ -29,14 +31,21 @@ public class Program
true)
.AddEnvironmentVariables()
.Build();
LoggingLevelSwitch = new LoggingLevelSwitch();
}
private static IConfiguration Configuration { get; }
private static LoggingLevelSwitch LoggingLevelSwitch { get; }
public static async Task<int> Main(string[] args)
{
LoggingLevelSwitch.MinimumLevel = LogEventLevel.Information;
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.MinimumLevel.ControlledBy(LoggingLevelSwitch)
.Destructure.UsingAttributes()
.Enrich.FromLogContext()
.WriteTo.SQLite(FileSystemLayout.LogDatabasePath, retentionPeriod: TimeSpan.FromDays(1))
@@ -61,6 +70,7 @@ public class Program
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddSingleton(LoggingLevelSwitch))
.ConfigureWebHostDefaults(
webBuilder => webBuilder.UseStartup<Startup>()
.UseConfiguration(Configuration)
@@ -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;
}
+1
View File
@@ -427,6 +427,7 @@ public class Startup
// services.AddTransient(typeof(IRequestHandler<,>), typeof(GetRecentLogEntriesHandler<>));
// run-once/blocking startup services
services.AddHostedService<LoadLoggingLevelService>();
services.AddHostedService<EndpointValidatorService>();
services.AddHostedService<DatabaseMigratorService>();
services.AddHostedService<CacheCleanerService>();