Files
xLoggingService/Writers/XEventLogWriter.cs
T
2026-11-08 17:57:08 +03:30

83 lines
2.4 KiB
C#

using System;
using System.Threading;
using xCommons.Extensions;
using xEventService.Models;
using System.Threading.Tasks;
using xLoggingService.Models;
using xCommons.Configurations;
using xEventService.Interfaces;
using System.Collections.Generic;
using xLoggingService.Interfaces;
using xLoggingService.Extensions;
using xLoggingService.Configuration;
namespace xLoggingService.Writers
{
public sealed class XEventLogWriter : IXLogWriter
{
//
private readonly XAppConfiguration appConfiguration;
private readonly XLoggingServiceConfiguration config;
private readonly Func<IXEventServiceProvider> resolver;
public XEventLogWriter(
XAppConfiguration appConfiguration,
XLoggingServiceConfiguration config,
Func<IXEventServiceProvider> resolver
)
{
//
this.config = config;
this.resolver = resolver;
this.appConfiguration = appConfiguration;
}
public async Task WriteAsync(
XLogEntry entry,
CancellationToken token
)
{
//
if (entry.IsNull())
{
return;
}
//
// Handle Task Enrichment ...
entry = entry.Enrich(appConfiguration);
//
var provider = resolver();
if (provider.IsNull() || entry.Level < config.Event.MinimumLevel)
{
return;
}
//
var payload = new Dictionary<string, string> {
{ nameof(XLogEntry.Category), entry.Category },
{ nameof(XLogEntry.Level), entry.Level.ToString() },
{ nameof(XLogEntry.State), entry.State ?? string.Empty },
{ nameof(XLogEntry.TraceId), entry.TraceId ?? string.Empty } ,
{ nameof(XLogEntry.Exception), entry.Exception ?? string.Empty },
};
//
await provider.SendMessageAsync(
new XEventMessage
{
Payload = payload,
Message = entry.Message,
Offset = entry.Timestamp,
Sender = config.Event.Sender,
Action = config.Event.Action,
},
config.Event.Topic,
token
).ConfigureAwait(false);
}
}
}