Initial Commit ...
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
#
|
||||||
|
# DotNet ...
|
||||||
|
bin
|
||||||
|
obj
|
||||||
|
|
||||||
|
#
|
||||||
|
# Natural Docs ...
|
||||||
|
Documentation/*
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xLoggingService.Models;
|
||||||
|
using xLoggingService.Interfaces;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
|
namespace xLoggingService.Background
|
||||||
|
{
|
||||||
|
public sealed class XLogDispatcher : IDisposable
|
||||||
|
{
|
||||||
|
private readonly Task worker;
|
||||||
|
private readonly BlockingCollection<XLogEntry> queue;
|
||||||
|
private readonly IReadOnlyCollection<IXLogWriter> writers;
|
||||||
|
private readonly CancellationTokenSource cancellation = new CancellationTokenSource();
|
||||||
|
|
||||||
|
public XLogDispatcher(
|
||||||
|
int capacity,
|
||||||
|
IEnumerable<IXLogWriter> writers
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
queue = new BlockingCollection<XLogEntry>(Math.Max(100, capacity));
|
||||||
|
this.writers = new List<IXLogWriter>(writers)
|
||||||
|
.AsReadOnly();
|
||||||
|
|
||||||
|
//
|
||||||
|
worker = Task.Run(RunAsync);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Enqueue(XLogEntry entry)
|
||||||
|
{
|
||||||
|
if (!queue.IsAddingCompleted && !queue.TryAdd(entry)) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task RunAsync()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
foreach (var entry in queue.GetConsumingEnumerable(cancellation.Token))
|
||||||
|
{
|
||||||
|
//
|
||||||
|
foreach (var writer in writers)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
await writer.WriteAsync(entry, cancellation.Token)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
queue.CompleteAdding();
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
worker.Wait(TimeSpan.FromSeconds(10));
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
//
|
||||||
|
cancellation.Cancel();
|
||||||
|
queue.Dispose();
|
||||||
|
cancellation.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xLoggingService.Configuration
|
||||||
|
{
|
||||||
|
public sealed class XLoggingServiceConfiguration
|
||||||
|
{
|
||||||
|
public bool Enabled { get; set; } = false;
|
||||||
|
public XFileLogConfiguration File { get; set; } = new();
|
||||||
|
public XLogQueueConfiguration Queue { get; set; } = new();
|
||||||
|
public XEventLogConfiguration Event { get; set; } = new();
|
||||||
|
public XDatabaseLogConfiguration Database { get; set; } = new();
|
||||||
|
public LogLevel MinimumLevel { get; set; } = LogLevel.Information;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class XLogQueueConfiguration {
|
||||||
|
public bool Enabled { get; set; }
|
||||||
|
public int Capacity { get; set; } = 10000;
|
||||||
|
public int ShutdownTimeoutSeconds { get; set; } = 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class XFileLogConfiguration {
|
||||||
|
public bool Enabled { get; set; }
|
||||||
|
public string Format { get; set; } = "Json";
|
||||||
|
public string Directory { get; set; } = "./Logs";
|
||||||
|
public int RetainedFileCountLimit { get; set; } = 30;
|
||||||
|
public string FileName { get; set; } = "x-logging-.log";
|
||||||
|
public LogLevel MinimumLevel { get; set; } = LogLevel.Information;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class XDatabaseLogConfiguration {
|
||||||
|
public bool Enabled { get; set; } = false;
|
||||||
|
public string ConnectionString { get; set; }
|
||||||
|
public string TableName { get; set; } = "XLogs";
|
||||||
|
public bool AutoCreateTable { get; set; } = true;
|
||||||
|
public LogLevel MinimumLevel { get; set; } = LogLevel.Warning;
|
||||||
|
public string Provider { get; set; } = "Microsoft.Data.Sqlite";
|
||||||
|
}
|
||||||
|
|
||||||
|
public class XEventLogConfiguration {
|
||||||
|
public bool Enabled { get; set; } = false;
|
||||||
|
public string Sender { get; set; } = "Application";
|
||||||
|
public string Action { get; set; } = "XLogCreated";
|
||||||
|
public LogLevel MinimumLevel { get; set; } = LogLevel.Error;
|
||||||
|
public string Topic { get; set; } = "x-logging-service-logs";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace xLoggingService.Constants
|
||||||
|
{
|
||||||
|
public partial struct ConfigurationNodeNames
|
||||||
|
{
|
||||||
|
public const string LOGGING_SERVICE_NODE = "LoggingServiceConfiguration";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace xLoggingService.Constants
|
||||||
|
{
|
||||||
|
public struct XLoggingServiceConstants
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Service Extensions Log Tag ...
|
||||||
|
public const string XLoggingServiceDILogTag = "XLoggingService";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xLoggingService.Writers;
|
||||||
|
using xEventService.Interfaces;
|
||||||
|
using xLoggingService.Providers;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xLoggingService.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xLoggingService.Configuration;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
|
using xLoggingService.Constants;
|
||||||
|
|
||||||
|
namespace xLoggingService.DI
|
||||||
|
{
|
||||||
|
public static class XDIHelperExtension
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Logging Service Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XLoggingServiceConfiguration GetXLoggingServiceConfiguration(
|
||||||
|
this IConfiguration source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var section = source
|
||||||
|
.GetSection(ConfigurationNodeNames.LOGGING_SERVICE_NODE);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = section.Get<XLoggingServiceConfiguration>();
|
||||||
|
if (result.IsNull())
|
||||||
|
{
|
||||||
|
result = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extract Service Configurations and Register it ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
public static void AddXLoggingServiceConfiguration(
|
||||||
|
this IServiceCollection source,
|
||||||
|
IConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var config = configuration.GetXLoggingServiceConfiguration();
|
||||||
|
source.AddXLoggingServiceConfiguration(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register Service Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
public static void AddXLoggingServiceConfiguration(
|
||||||
|
this IServiceCollection source,
|
||||||
|
XLoggingServiceConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (configuration.IsNull())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Log($"Invalid Configuration ...");
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
source.AddSingleton(configuration);
|
||||||
|
Log($"Logging Configuration Registered Successfully ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register Logging Service ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
public static void AddXLoggingService(
|
||||||
|
this IServiceCollection source,
|
||||||
|
IConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var config = configuration.GetXLoggingServiceConfiguration();
|
||||||
|
source.AddLoggingService(
|
||||||
|
configuration: config,
|
||||||
|
lifeTime: ServiceLifetime.Scoped
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private
|
||||||
|
/// <summary>
|
||||||
|
/// a LogTag for Service ...
|
||||||
|
/// </summary>
|
||||||
|
private static string XLogTag = XLoggingServiceConstants.XLoggingServiceDILogTag;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// print a log in Console ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
private static void Log(string message)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{XLogTag} => {message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void AddLoggingService(
|
||||||
|
this IServiceCollection services,
|
||||||
|
XLoggingServiceConfiguration configuration,
|
||||||
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (lifeTime.IsNull())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Log($"Invalid Service Lifetime ...");
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// Register Service Configuration ...
|
||||||
|
services.AddXLoggingServiceConfiguration(configuration);
|
||||||
|
|
||||||
|
//
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using xCommons.Extensions;
|
||||||
|
using xLoggingService.Models;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
|
||||||
|
namespace xLoggingService.Extensions
|
||||||
|
{
|
||||||
|
public static class XLoggingServiceExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Enrich LogEntry ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="configuration"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XLogEntry Enrich(
|
||||||
|
this XLogEntry source,
|
||||||
|
XAppConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (source.IsNull() || configuration.IsNull())
|
||||||
|
{
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
source.Application = configuration.Name;
|
||||||
|
|
||||||
|
//
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xLoggingService.Models;
|
||||||
|
|
||||||
|
namespace xLoggingService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXLogWriter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Write Log ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entry"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task WriteAsync(
|
||||||
|
XLogEntry entry,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace xLoggingService.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// a Log which Issued ...
|
||||||
|
/// </summary>
|
||||||
|
public class XLogEntry
|
||||||
|
{
|
||||||
|
public int EventId { get; set; }
|
||||||
|
public string State { get; set; }
|
||||||
|
public string SpanId { get; set; }
|
||||||
|
public LogLevel Level { get; set; }
|
||||||
|
public string Message { get; set; }
|
||||||
|
public string TraceId { get; set; }
|
||||||
|
public string Category { get; set; }
|
||||||
|
public string Exception { get; set; }
|
||||||
|
public DateTime Timestamp { get; set; }
|
||||||
|
public string Application { get; set; }
|
||||||
|
public string Environment { get; set; }
|
||||||
|
public string MachineName { get; set; }
|
||||||
|
public string CorrelationId { get; set; }
|
||||||
|
public IDictionary<string, string> Scope { get; set; } = new Dictionary<string, string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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() { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
# xLoggingService
|
||||||
|
|
||||||
|
provide all commonly used actions/extensions and classes which required to Logging/Tracing Feature in xDashboard project.
|
||||||
|
|
||||||
|
## Implementing
|
||||||
|
|
||||||
|
after adding this package as a dependency to your project you can do following:
|
||||||
|
|
||||||
|
### 1. Register it on DI
|
||||||
|
|
||||||
|
```c#
|
||||||
|
```
|
||||||
|
|
||||||
|
<!-- TOOD: Complete this ... -->
|
||||||
|
|
||||||
|
## Maintainer
|
||||||
|
|
||||||
|
Hadi Khazaee asl
|
||||||
|
|
||||||
|
[https://www.saherelm.ir](https://www.saherelm.ir)
|
||||||
|
|
||||||
|
[hadi_khazaee_asl@yahoo.com](mailto:hadi_khazaee_asl@yahoo.com)
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Data.Common;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xLoggingService.Models;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using xLoggingService.Interfaces;
|
||||||
|
using xLoggingService.Extensions;
|
||||||
|
using xLoggingService.Configuration;
|
||||||
|
|
||||||
|
namespace xLoggingService.Writers
|
||||||
|
{
|
||||||
|
public sealed class XDatabaseLogWriter : IXLogWriter
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private bool initialized;
|
||||||
|
private readonly object sync = new();
|
||||||
|
private readonly XDatabaseLogConfiguration config;
|
||||||
|
private readonly XAppConfiguration appConfiguration;
|
||||||
|
|
||||||
|
public XDatabaseLogWriter(
|
||||||
|
XDatabaseLogConfiguration config,
|
||||||
|
XAppConfiguration appConfiguration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
this.config = config;
|
||||||
|
this.appConfiguration = appConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task WriteAsync(
|
||||||
|
XLogEntry e,
|
||||||
|
CancellationToken token
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (e.IsNull())
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle Task Enrichment ...
|
||||||
|
e = e.Enrich(appConfiguration);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (e.Level < config.MinimumLevel || config.ConnectionString.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
lock (sync)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
using (var c = CreateConnection())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
c.Open();
|
||||||
|
if (!initialized)
|
||||||
|
{
|
||||||
|
using (var cmd = c.CreateCommand())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
cmd.CommandText = $"CREATE TABLE IF NOT EXISTS [{config.TableName}] (Id INTEGER PRIMARY KEY, Timestamp TEXT, Level TEXT, Category TEXT, EventId INTEGER, Message TEXT, Exception TEXT, State TEXT, Application TEXT, Environment TEXT, MachineName TEXT, TraceId TEXT, SpanId TEXT, CorrelationId TEXT)";
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
using (var cmd = c.CreateCommand())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
cmd.CommandText = $"INSERT INTO [{config.TableName}] (Timestamp,Level,Category,EventId,Message,Exception,State,Application,Environment,MachineName,TraceId,SpanId,CorrelationId) VALUES (@Timestamp,@Level,@Category,@EventId,@Message,@Exception,@State,@Application,@Environment,@MachineName,@TraceId,@SpanId,@CorrelationId)";
|
||||||
|
|
||||||
|
//
|
||||||
|
Add(cmd, "@State", e.State);
|
||||||
|
Add(cmd, "@SpanId", e.SpanId);
|
||||||
|
Add(cmd, "@EventId", e.EventId);
|
||||||
|
Add(cmd, "@Message", e.Message);
|
||||||
|
Add(cmd, "@TraceId", e.TraceId);
|
||||||
|
Add(cmd, "@Category", e.Category);
|
||||||
|
Add(cmd, "@Exception", e.Exception);
|
||||||
|
Add(cmd, "@Level", e.Level.ToString());
|
||||||
|
Add(cmd, "@Application", e.Application);
|
||||||
|
Add(cmd, "@Environment", e.Environment);
|
||||||
|
Add(cmd, "@MachineName", e.MachineName);
|
||||||
|
Add(cmd, "@CorrelationId", e.CorrelationId);
|
||||||
|
Add(cmd, "@Timestamp", e.Timestamp.ToString("O"));
|
||||||
|
|
||||||
|
//
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
//
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DbConnection CreateConnection()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var typeName =
|
||||||
|
config.Provider == "Microsoft.Data.Sqlite"
|
||||||
|
? "Microsoft.Data.Sqlite.SqliteConnection, Microsoft.Data.Sqlite"
|
||||||
|
: config.Provider == "System.Data.SqlClient" ?
|
||||||
|
"System.Data.SqlClient.SqlConnection, System.Data.SqlClient"
|
||||||
|
: config.Provider;
|
||||||
|
|
||||||
|
//
|
||||||
|
var type = Type.GetType(typeName, false);
|
||||||
|
if (type.IsNull())
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Database provider was not found: " + typeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var c = (DbConnection)Activator.CreateInstance(type);
|
||||||
|
c.ConnectionString = config.ConnectionString;
|
||||||
|
|
||||||
|
//
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Add(
|
||||||
|
DbCommand c,
|
||||||
|
string name,
|
||||||
|
object value
|
||||||
|
) {
|
||||||
|
//
|
||||||
|
var p = c.CreateParameter();
|
||||||
|
p.ParameterName = name;
|
||||||
|
p.Value = (object)value ?? DBNull.Value;
|
||||||
|
c.Parameters.Add(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xLoggingService.Models;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using xLoggingService.Interfaces;
|
||||||
|
using xLoggingService.Configuration;
|
||||||
|
using xLoggingService.Extensions;
|
||||||
|
|
||||||
|
namespace xLoggingService.Writers
|
||||||
|
{
|
||||||
|
public sealed class XFileLogWriter : IXLogWriter
|
||||||
|
{
|
||||||
|
//
|
||||||
|
private readonly object sync = new();
|
||||||
|
private readonly XAppConfiguration appConfiguration;
|
||||||
|
private readonly XLoggingServiceConfiguration configuration;
|
||||||
|
|
||||||
|
public XFileLogWriter(
|
||||||
|
XAppConfiguration appConfiguration,
|
||||||
|
XLoggingServiceConfiguration configuration
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (configuration.IsNull() || appConfiguration.IsNull())
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
this.configuration = configuration;
|
||||||
|
this.appConfiguration = appConfiguration;
|
||||||
|
|
||||||
|
//
|
||||||
|
Directory.CreateDirectory(configuration.File.Directory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task WriteAsync(
|
||||||
|
XLogEntry entry,
|
||||||
|
CancellationToken token
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (entry.IsNull())
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle Task Enrichment ...
|
||||||
|
entry = entry.Enrich(appConfiguration);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (entry.Level < configuration.File.MinimumLevel)
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var path = Path.Combine(configuration.File.Directory, configuration.File.FileName.Replace("-", "-"));
|
||||||
|
|
||||||
|
//
|
||||||
|
lock (sync)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
File.AppendAllText(path, configuration.File.Format.Equals(
|
||||||
|
"Json",
|
||||||
|
StringComparison.OrdinalIgnoreCase)
|
||||||
|
? JsonConvert.SerializeObject(entry) + Environment.NewLine
|
||||||
|
: $"{entry.Timestamp:O} [{entry.Level}] {entry.Category}: {entry.Message}{Environment.NewLine}", Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<configuration>
|
||||||
|
<packageSources>
|
||||||
|
<!-- <add key="megan" value="https://hub.megan.ir/nuget/index.json" /> -->
|
||||||
|
<add key="nuget" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||||
|
<!-- <add key="baget" value="https://nuget.saherelmhub.ir/v3/index.json" protocolVersion="3" disableTLSCertificateValidation="true" /> -->
|
||||||
|
</packageSources>
|
||||||
|
</configuration>
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<!-- Runtime Definition -->
|
||||||
|
<PropertyGroup>
|
||||||
|
<Version>1.0.0</Version>
|
||||||
|
<LangVersion>12.0</LangVersion>
|
||||||
|
<Authors>Hadi Khazaee Asl</Authors>
|
||||||
|
<Company>SaherElm IT Center</Company>
|
||||||
|
<PackageId>xDashboard.xLoggingService</PackageId>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
|
||||||
|
<Description>
|
||||||
|
provide all commonly used actions/extensions and classes which required to Logging/Tracing Feature in xDashboard project.
|
||||||
|
</Description>
|
||||||
|
|
||||||
|
<!-- Icon Definition -->
|
||||||
|
<PackageIcon>icon.png</PackageIcon>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<!-- Icon Handling -->
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="../../Documents/Resources/Images/favicon.png" Link="icon.png" Pack="true"
|
||||||
|
PackagePath="\icon.png" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- Local Modules -->
|
||||||
|
<ItemGroup>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- Local Dependencies -->
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\xEventService\xEventService.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- Dependencies -->
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- For XML Documentation Support -->
|
||||||
|
<PropertyGroup>
|
||||||
|
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
|
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user