Initial Commit ...
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using xLoggingService.Models;
|
||||
using xCommons.Configurations;
|
||||
using System.Collections.Generic;
|
||||
using xLoggingService.Background;
|
||||
using xLoggingService.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xLoggingService.Configuration;
|
||||
|
||||
namespace xLoggingService.Providers
|
||||
{
|
||||
public class XLoggerProvider : ILoggerProvider
|
||||
{
|
||||
//
|
||||
private readonly XLogDispatcher dispatcher;
|
||||
private readonly XAppConfiguration configuration;
|
||||
private readonly XLoggingServiceConfiguration config;
|
||||
private readonly IReadOnlyCollection<IXLogWriter> writers;
|
||||
|
||||
public XLoggerProvider(
|
||||
XAppConfiguration configuration,
|
||||
XLoggingServiceConfiguration config,
|
||||
IEnumerable<IXLogWriter> writers
|
||||
)
|
||||
{
|
||||
//
|
||||
this.config = config;
|
||||
this.configuration = configuration;
|
||||
this.writers = new List<IXLogWriter>(writers)
|
||||
.AsReadOnly();
|
||||
|
||||
//
|
||||
dispatcher = new XLogDispatcher(
|
||||
config.Queue.Capacity, this.writers);
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
//
|
||||
return new XLogger(
|
||||
config: config,
|
||||
category: categoryName,
|
||||
dispatcher: dispatcher,
|
||||
configuration: configuration
|
||||
);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
dispatcher.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public class XLogger : ILogger
|
||||
{
|
||||
//
|
||||
private readonly string category;
|
||||
private readonly XLogDispatcher dispatcher;
|
||||
private readonly XAppConfiguration configuration;
|
||||
private readonly XLoggingServiceConfiguration config;
|
||||
|
||||
public XLogger(
|
||||
string category,
|
||||
XLogDispatcher dispatcher,
|
||||
XAppConfiguration configuration,
|
||||
XLoggingServiceConfiguration config
|
||||
)
|
||||
{
|
||||
//
|
||||
this.config = config;
|
||||
this.category = category;
|
||||
this.dispatcher = dispatcher;
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
return NullScope.Instance;
|
||||
}
|
||||
|
||||
public bool IsEnabled(LogLevel level)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
config.Enabled &&
|
||||
level >= config.MinimumLevel &&
|
||||
level != LogLevel.None;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Log<TState>(
|
||||
LogLevel level,
|
||||
EventId eventId,
|
||||
TState state,
|
||||
Exception exception,
|
||||
Func<TState, Exception, string> formatter
|
||||
)
|
||||
{
|
||||
//
|
||||
if (!IsEnabled(level))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
var activity = Activity.Current;
|
||||
dispatcher.Enqueue(new XLogEntry
|
||||
{
|
||||
Level = level,
|
||||
Category = category,
|
||||
EventId = eventId.Id,
|
||||
TraceId = activity?.Id,
|
||||
SpanId = activity?.RootId,
|
||||
Timestamp = DateTime.UtcNow,
|
||||
Message = formatter == null
|
||||
? state == null
|
||||
? string.Empty
|
||||
: state.ToString()
|
||||
: formatter(state, exception),
|
||||
Exception = exception == null
|
||||
? null
|
||||
: exception.ToString(),
|
||||
State = state == null
|
||||
? null :
|
||||
JsonConvert.SerializeObject(state),
|
||||
Application = configuration.Name,
|
||||
MachineName = Environment.MachineName,
|
||||
Environment = configuration.Environment,
|
||||
});
|
||||
}
|
||||
|
||||
private sealed class NullScope : IDisposable {
|
||||
public static readonly NullScope Instance = new();
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user