//-----------------------------------------------------------------------
//
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.Logging;
using System;
using AliasServerDb;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Serilog.Core;
using Serilog.Events;
///
/// Custom Serilog sink for Database via Entity Framework Core.
///
/// IFormatProvider instance.
/// DB context factory to use.
/// Application name.
public class DatabaseSink(IFormatProvider formatProvider, Func> contextFactory, string applicationName) : ILogEventSink
{
///
/// Saves the log event to the database.
///
/// LogEvent instance.
public void Emit(LogEvent logEvent)
{
var logEntry = new Log
{
TimeStamp = logEvent.Timestamp.UtcDateTime,
Level = logEvent.Level.ToString(),
Message = logEvent.RenderMessage(formatProvider),
Exception = logEvent.Exception?.ToString() ?? string.Empty,
Properties = JsonConvert.SerializeObject(logEvent.Properties),
LogEvent = (logEvent.Level >= LogEventLevel.Error) ? JsonConvert.SerializeObject(logEvent) : string.Empty,
MessageTemplate = logEvent.MessageTemplate.Text,
Application = applicationName,
SourceContext = logEvent.Properties.ContainsKey("SourceContext")
? logEvent.Properties["SourceContext"].ToString().Trim('"')
: string.Empty,
};
try
{
using var context = contextFactory.Invoke().CreateDbContext();
context.Logs.Add(logEntry);
context.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine($"Error writing log entry to database: {ex}");
}
}
}