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