Initial Commit ...
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user