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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user