Setting up a db factory with logging
Important so we can log all commands
This commit is contained in:
parent
ad4ed12f00
commit
1501ff442a
10 changed files with 412 additions and 177 deletions
70
Database/Core/ConnectionFactory/Class1.cs
Normal file
70
Database/Core/ConnectionFactory/Class1.cs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
using Npgsql;
|
||||
using PlanTempus.Database.ModuleRegistry;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PlanTempus.Database.Core.ConnectionFactory
|
||||
{
|
||||
public class PostgresConnectionFactory : IDbConnectionFactory, IAsyncDisposable
|
||||
{
|
||||
private readonly NpgsqlDataSource _baseDataSource;
|
||||
private readonly Action<NpgsqlDataSourceBuilder> _configureDataSource;
|
||||
private readonly Microsoft.Extensions.Logging.ILoggerFactory _loggerFactory;
|
||||
|
||||
public PostgresConnectionFactory(
|
||||
string connectionString,
|
||||
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory = null,
|
||||
Action<NpgsqlDataSourceBuilder> configureDataSource = null)
|
||||
{
|
||||
_loggerFactory = loggerFactory;
|
||||
_configureDataSource = configureDataSource ?? (builder => { });
|
||||
|
||||
// Opret base data source med konfiguration
|
||||
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
|
||||
ConfigureDataSourceBuilder(dataSourceBuilder);
|
||||
_baseDataSource = dataSourceBuilder.Build();
|
||||
}
|
||||
|
||||
public IDbConnection Create()
|
||||
{
|
||||
return _baseDataSource.CreateConnection();
|
||||
}
|
||||
|
||||
public IDbConnection Create(string username, string password)
|
||||
{
|
||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(
|
||||
_baseDataSource.ConnectionString)
|
||||
{
|
||||
Username = username,
|
||||
Password = password
|
||||
};
|
||||
|
||||
var tempDataSourceBuilder = new NpgsqlDataSourceBuilder(
|
||||
connectionStringBuilder.ToString());
|
||||
|
||||
ConfigureDataSourceBuilder(tempDataSourceBuilder);
|
||||
|
||||
var tempDataSource = tempDataSourceBuilder.Build();
|
||||
return tempDataSource.CreateConnection();
|
||||
}
|
||||
|
||||
private void ConfigureDataSourceBuilder(NpgsqlDataSourceBuilder builder)
|
||||
{
|
||||
if (_loggerFactory != null)
|
||||
{
|
||||
builder.UseLoggerFactory(_loggerFactory);
|
||||
}
|
||||
|
||||
_configureDataSource?.Invoke(builder);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _baseDataSource.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
102
Database/Core/Sql/SqlOperations.cs
Normal file
102
Database/Core/Sql/SqlOperations.cs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using PlanTempus.Database.ModuleRegistry;
|
||||
using System.Data;
|
||||
|
||||
namespace PlanTempus.Database.Core.Sql
|
||||
{
|
||||
public class DatabaseScope : IDisposable
|
||||
{
|
||||
private readonly IDbConnection _connection;
|
||||
private readonly IOperationHolder<DependencyTelemetry> _operation;
|
||||
|
||||
public DatabaseScope(IDbConnection connection, IOperationHolder<DependencyTelemetry> operation)
|
||||
{
|
||||
_connection = connection;
|
||||
_operation = operation;
|
||||
}
|
||||
|
||||
public IDbConnection Connection => _connection;
|
||||
|
||||
public void Success()
|
||||
{
|
||||
_operation.Telemetry.Success = true;
|
||||
}
|
||||
|
||||
public void Error(Exception ex)
|
||||
{
|
||||
_operation.Telemetry.Success = false;
|
||||
_operation.Telemetry.Properties["Error"] = ex.Message;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_operation.Dispose();
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IDatabaseOperations
|
||||
{
|
||||
DatabaseScope CreateScope(string operationName);
|
||||
Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName);
|
||||
Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName);
|
||||
}
|
||||
|
||||
public class SqlOperations : IDatabaseOperations
|
||||
{
|
||||
private readonly IDbConnectionFactory _connectionFactory;
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
|
||||
public SqlOperations(IDbConnectionFactory connectionFactory, TelemetryClient telemetryClient)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
_telemetryClient = telemetryClient;
|
||||
}
|
||||
|
||||
public DatabaseScope CreateScope(string operationName)
|
||||
{
|
||||
var connection = _connectionFactory.Create();
|
||||
var operation = _telemetryClient.StartOperation<DependencyTelemetry>(operationName);
|
||||
operation.Telemetry.Type = "SQL";
|
||||
operation.Telemetry.Target = "PostgreSQL";
|
||||
|
||||
return new DatabaseScope(connection, operation);
|
||||
}
|
||||
|
||||
public async Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName)
|
||||
{
|
||||
using var scope = CreateScope(operationName);
|
||||
try
|
||||
{
|
||||
var result = await operation(scope.Connection);
|
||||
scope.Success();
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
scope.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName)
|
||||
{
|
||||
using var scope = CreateScope(operationName);
|
||||
try
|
||||
{
|
||||
await operation(scope.Connection);
|
||||
scope.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
scope.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -3,20 +3,55 @@ using Npgsql;
|
|||
using System.Data;
|
||||
namespace PlanTempus.Database.ModuleRegistry
|
||||
{
|
||||
public interface IDbConnectionFactory
|
||||
{
|
||||
IDbConnection Create();
|
||||
IDbConnection Create(string username, string password);
|
||||
}
|
||||
public class DbPostgreSqlModule : Module
|
||||
{
|
||||
public required string ConnectionString { get; set; }
|
||||
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
||||
|
||||
builder.Register(c =>
|
||||
{
|
||||
IDbConnection connection = new NpgsqlConnection(ConnectionString);
|
||||
return connection;
|
||||
})
|
||||
.InstancePerLifetimeScope();
|
||||
builder.Register<IDbConnectionFactory>(c =>
|
||||
new Core.ConnectionFactory.PostgresConnectionFactory(ConnectionString))
|
||||
.SingleInstance();
|
||||
|
||||
builder.RegisterType<Core.Sql.SqlOperations>()
|
||||
.As<Core.Sql.IDatabaseOperations>();
|
||||
}
|
||||
}
|
||||
public class PostgresConnectionFactory1 //: IDbConnectionFactory
|
||||
{
|
||||
private readonly string _baseConnectionString;
|
||||
|
||||
public PostgresConnectionFactory1(string connectionString)
|
||||
{
|
||||
_baseConnectionString = connectionString;
|
||||
}
|
||||
|
||||
public IDbConnection Create()
|
||||
{
|
||||
return new NpgsqlConnection(_baseConnectionString);
|
||||
}
|
||||
|
||||
public IDbConnection Create(string username, string password)
|
||||
{
|
||||
var builder = new NpgsqlConnectionStringBuilder(_baseConnectionString)
|
||||
{
|
||||
Username = username,
|
||||
Password = password
|
||||
};
|
||||
|
||||
return new NpgsqlConnection(builder.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue