Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update DatabaseSink logic (#113)

Leendert de Borstfc8f935092d9
diff --git a/src/Services/AliasVault.SmtpService/Worker.cs b/src/Services/AliasVault.SmtpService/Worker.cs
index 3ca9c97fc..2d6f168ca 100644
--- a/src/Services/AliasVault.SmtpService/Worker.cs
+++ b/src/Services/AliasVault.SmtpService/Worker.cs
@@ -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)
{
diff --git a/src/Utilities/AliasVault.Logging/AliasVault.Logging.csproj b/src/Utilities/AliasVault.Logging/AliasVault.Logging.csproj
index fa7cd792d..c58d344b9 100644
--- a/src/Utilities/AliasVault.Logging/AliasVault.Logging.csproj
+++ b/src/Utilities/AliasVault.Logging/AliasVault.Logging.csproj
@@ -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" />
diff --git a/src/Utilities/AliasVault.Logging/DatabaseSink.cs b/src/Utilities/AliasVault.Logging/DatabaseSink.cs
index 9a100016f..a0c06605e 100644
--- a/src/Utilities/AliasVault.Logging/DatabaseSink.cs
+++ b/src/Utilities/AliasVault.Logging/DatabaseSink.cs
@@ -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,
};
diff --git a/src/Utilities/AliasVault.Logging/LoggingConfiguration.cs b/src/Utilities/AliasVault.Logging/LoggingConfiguration.cs
index acce42f24..47ddefaa4 100644
--- a/src/Utilities/AliasVault.Logging/LoggingConfiguration.cs
+++ b/src/Utilities/AliasVault.Logging/LoggingConfiguration.cs
@@ -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);
}