Update DatabaseSink logic (#113)

This commit is contained in:
Leendert de Borst
2024-07-24 22:11:08 +02:00
parent d5cf51b5da
commit fc8f935092
4 changed files with 14 additions and 16 deletions

View File

@@ -5,12 +5,9 @@
// </copyright>
//-----------------------------------------------------------------------
using AliasServerDb;
using Microsoft.EntityFrameworkCore;
namespace AliasVault.SmtpService;
public class Worker(ILogger<Worker> logger, SmtpServer.SmtpServer smtpServer, IDbContextFactory<AliasServerDbContext> contextFactory) : BackgroundService
public class Worker(ILogger<Worker> logger, SmtpServer.SmtpServer smtpServer) : BackgroundService
{
/// <inheritdoc />
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -25,11 +22,11 @@ public class Worker(ILogger<Worker> logger, SmtpServer.SmtpServer smtpServer, ID
// Wait for the cancellation token to be triggered
await Task.Delay(Timeout.Infinite, stoppingToken);
}
catch (OperationCanceledException)
catch (OperationCanceledException ex)
{
// This exception is thrown when the stoppingToken is canceled
// It's expected behavior, so we can just log it
logger.LogWarning("AliasVault.SmtpService is stopping due to a cancellation request");
logger.LogWarning(ex, "AliasVault.SmtpService is stopping due to a cancellation request.");
}
catch (Exception ex)
{

View File

@@ -22,6 +22,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />

View File

@@ -10,6 +10,7 @@ namespace AliasVault.Logging;
using System;
using AliasServerDb;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Serilog.Core;
using Serilog.Events;
@@ -33,9 +34,9 @@ public class DatabaseSink(IFormatProvider formatProvider, Func<IDbContextFactory
Level = logEvent.Level.ToString(),
Message = logEvent.RenderMessage(formatProvider),
Exception = logEvent.Exception?.ToString() ?? string.Empty,
Properties = logEvent.Properties.ToString() ?? string.Empty,
LogEvent = string.Empty,
MessageTemplate = string.Empty,
Properties = JsonConvert.SerializeObject(logEvent.Properties),
LogEvent = (logEvent.Level >= LogEventLevel.Error) ? JsonConvert.SerializeObject(logEvent) : string.Empty,
MessageTemplate = logEvent.MessageTemplate.Text,
Application = applicationName,
};

View File

@@ -87,16 +87,15 @@ public static class LoggingConfiguration
{
var logLevels = configuration.GetSection("Logging:LogLevel");
// Check if this specific source context has an override level defined in appsettings.json.
foreach (var logLevel in logLevels.GetChildren())
// If this specific source context has an override level defined in appsettings.json, return that.
var logLevel = logLevels.GetChildren()
.FirstOrDefault(ll => sourceContext.Contains(ll.Key, StringComparison.OrdinalIgnoreCase));
if (logLevel != null)
{
if (sourceContext.Contains(logLevel.Key, StringComparison.OrdinalIgnoreCase))
{
return Enum.Parse<LogEventLevel>(logLevel.Value ?? "Information", true);
}
return Enum.Parse<LogEventLevel>(logLevel.Value ?? "Information", true);
}
// If no specific override, use the default.
// If there is no specific override, use the default.
var defaultLevel = logLevels["Default"] ?? "Information";
return Enum.Parse<LogEventLevel>(defaultLevel, true);
}