techgarage-ir.MTWireGuard/Application/ClientIdEnricher.cs
Tech Garage b128154c60 Added Serilog to manage logs
Handle required fields while starting
2024-07-21 00:31:22 +03:30

30 lines
875 B
C#

using Serilog.Configuration;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MTWireGuard.Application
{
public class ClientIdEnricher(string clientId) : ILogEventEnricher
{
private readonly string _clientId = clientId;
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("ClientId", _clientId));
}
}
public static class LoggingExtensions
{
public static LoggerConfiguration WithClientId(this LoggerEnrichmentConfiguration enrichmentConfiguration, string clientId)
{
return enrichmentConfiguration.With(new ClientIdEnricher(clientId));
}
}
}