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
|
|
@ -3,7 +3,7 @@
|
||||||
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id={usr};Password={pwd};"
|
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id={usr};Password={pwd};"
|
||||||
},
|
},
|
||||||
"ApplicationInsights": {
|
"ApplicationInsights": {
|
||||||
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
|
"ConnectionString": "InstrumentationKey=07d2a2b9-5e8e-4924-836e-264f8438f6c5;IngestionEndpoint=https://northeurope-2.in.applicationinsights.azure.com/;LiveEndpoint=https://northeurope.livediagnostics.monitor.azure.com/;ApplicationId=56748c39-2fa3-4880-a1e2-24068e791548",
|
||||||
"UseSeqLoggingTelemetryChannel": true
|
"UseSeqLoggingTelemetryChannel": true
|
||||||
},
|
},
|
||||||
"SeqConfiguration": {
|
"SeqConfiguration": {
|
||||||
|
|
|
||||||
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;
|
using System.Data;
|
||||||
namespace PlanTempus.Database.ModuleRegistry
|
namespace PlanTempus.Database.ModuleRegistry
|
||||||
{
|
{
|
||||||
|
public interface IDbConnectionFactory
|
||||||
|
{
|
||||||
|
IDbConnection Create();
|
||||||
|
IDbConnection Create(string username, string password);
|
||||||
|
}
|
||||||
public class DbPostgreSqlModule : Module
|
public class DbPostgreSqlModule : Module
|
||||||
{
|
{
|
||||||
public required string ConnectionString { get; set; }
|
public required string ConnectionString { get; set; }
|
||||||
|
|
||||||
protected override void Load(ContainerBuilder builder)
|
protected override void Load(ContainerBuilder builder)
|
||||||
{
|
{
|
||||||
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
||||||
|
|
||||||
builder.Register(c =>
|
builder.Register<IDbConnectionFactory>(c =>
|
||||||
{
|
new Core.ConnectionFactory.PostgresConnectionFactory(ConnectionString))
|
||||||
IDbConnection connection = new NpgsqlConnection(ConnectionString);
|
.SingleInstance();
|
||||||
return connection;
|
|
||||||
})
|
|
||||||
.InstancePerLifetimeScope();
|
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,82 +29,28 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
|
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
Welcome();
|
|
||||||
string userPass;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
do
|
var host = Host.CreateDefaultBuilder(args)
|
||||||
{
|
.UseEnvironment("Development")
|
||||||
Console.WriteLine("Input username:password for a superadmin role");
|
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
|
||||||
userPass = Console.ReadLine() ?? string.Empty;
|
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
|
||||||
} while (!userPass.Contains(":") || userPass.Split(":").Length != 2 ||
|
{
|
||||||
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
|
var startup = new Startup();
|
||||||
string.IsNullOrEmpty(userPass.Split(":")[1]));
|
startup.ConfigureContainer();
|
||||||
|
})
|
||||||
|
|
||||||
var ctp = new Startup.ConnectionStringTemplateParameters(
|
.Build();
|
||||||
user: userPass.Split(":")[0],
|
|
||||||
pwd: userPass.Split(":")[1]
|
|
||||||
);
|
|
||||||
_container = new Startup().ConfigureContainer(ctp);
|
|
||||||
|
|
||||||
if (IsSuperAdmin())
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
|
||||||
var sw = new Stopwatch();
|
|
||||||
|
|
||||||
Console.Write("Database.Core.DCL.SetupDbAdmin...");
|
|
||||||
sw.Start();
|
|
||||||
var setupDbAdmin = _container.Resolve<SetupDbAdmin>();
|
|
||||||
setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
|
|
||||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine("::");
|
|
||||||
Console.WriteLine("::");
|
|
||||||
Console.Write("Database.Core.DDL.SetupIdentitySystem...");
|
|
||||||
sw.Restart();
|
|
||||||
|
|
||||||
//create new container with application user, we use that role from now.
|
|
||||||
_container = new Startup().ConfigureContainer(new Startup.ConnectionStringTemplateParameters("heimdall", "3911"));
|
|
||||||
|
|
||||||
var setupIdentitySystem = _container.Resolve<SetupIdentitySystem>();
|
|
||||||
setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" });
|
|
||||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
|
||||||
|
|
||||||
Console.WriteLine("::");
|
|
||||||
Console.WriteLine("::");
|
|
||||||
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
|
|
||||||
sw.Restart();
|
|
||||||
var setupConfigurationSystem = _container.Resolve<SetupConfiguration>();
|
|
||||||
setupConfigurationSystem.With(new SetupConfiguration.Command());
|
|
||||||
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine("::");
|
|
||||||
Console.WriteLine("::");
|
|
||||||
Console.Write("Database.Core.DCL.SetupApplicationUser...");
|
|
||||||
sw.Start();
|
|
||||||
var setupApplicationUser = _container.Resolve<SetupApplicationUser>();
|
|
||||||
setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
|
|
||||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
|
||||||
|
|
||||||
|
|
||||||
//and a lot of other tables that we haven't defined yet
|
|
||||||
|
|
||||||
// input configurations!!! TODO:Missing
|
|
||||||
|
|
||||||
Console.ForegroundColor = _foregroundColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
await host.StartAsync();
|
||||||
|
Console.WriteLine("Host has started.");
|
||||||
|
Run();
|
||||||
|
|
||||||
|
await host.WaitForShutdownAsync();
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
catch (Exception e)
|
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.WriteLine($"Host failed to start: {ex}");
|
||||||
Console.WriteLine(e);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -112,8 +58,6 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
|
|
||||||
static bool IsSuperAdmin()
|
static bool IsSuperAdmin()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
//test db access
|
//test db access
|
||||||
Console.WriteLine("Testing db access...");
|
Console.WriteLine("Testing db access...");
|
||||||
|
|
||||||
|
|
@ -191,34 +135,90 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task Run(string[] args)
|
static void Run()
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
var host = Host.CreateDefaultBuilder(args)
|
Welcome();
|
||||||
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
|
string userPass;
|
||||||
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
|
|
||||||
{
|
try
|
||||||
var startup = new Startup();
|
{
|
||||||
var connectionStringParams = new Startup.ConnectionStringTemplateParameters("your_user", "your_password");
|
do
|
||||||
startup.ConfigureContainer(connectionStringParams);
|
{
|
||||||
})
|
Console.WriteLine("Input username:password for a superadmin role");
|
||||||
.ConfigureServices((hostContext, services) =>
|
userPass = Console.ReadLine() ?? string.Empty;
|
||||||
{
|
} while (!userPass.Contains(":") || userPass.Split(":").Length != 2 ||
|
||||||
// Konfigurer andre services her (hvis nødvendigt)
|
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
|
||||||
})
|
string.IsNullOrEmpty(userPass.Split(":")[1]));
|
||||||
.Build();
|
|
||||||
|
//var ctp = new Startup.ConnectionStringTemplateParameters(
|
||||||
|
// user: userPass.Split(":")[0],
|
||||||
|
// pwd: userPass.Split(":")[1]
|
||||||
|
//);
|
||||||
|
//_container = new Startup().ConfigureContainer(ctp);
|
||||||
|
|
||||||
|
if (IsSuperAdmin())
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
var sw = new Stopwatch();
|
||||||
|
|
||||||
|
Console.Write("Database.Core.DCL.SetupDbAdmin...");
|
||||||
|
sw.Start();
|
||||||
|
var setupDbAdmin = _container.Resolve<SetupDbAdmin>();
|
||||||
|
setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
|
||||||
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine("::");
|
||||||
|
Console.WriteLine("::");
|
||||||
|
Console.Write("Database.Core.DDL.SetupIdentitySystem...");
|
||||||
|
sw.Restart();
|
||||||
|
|
||||||
|
//create new container with application user, we use that role from now.
|
||||||
|
//_container = new Startup().ConfigureContainer(new Startup.ConnectionStringTemplateParameters("heimdall", "3911"));
|
||||||
|
|
||||||
|
var setupIdentitySystem = _container.Resolve<SetupIdentitySystem>();
|
||||||
|
setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" });
|
||||||
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
|
Console.WriteLine("::");
|
||||||
|
Console.WriteLine("::");
|
||||||
|
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
|
||||||
|
sw.Restart();
|
||||||
|
var setupConfigurationSystem = _container.Resolve<SetupConfiguration>();
|
||||||
|
setupConfigurationSystem.With(new SetupConfiguration.Command());
|
||||||
|
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine("::");
|
||||||
|
Console.WriteLine("::");
|
||||||
|
Console.Write("Database.Core.DCL.SetupApplicationUser...");
|
||||||
|
sw.Start();
|
||||||
|
var setupApplicationUser = _container.Resolve<SetupApplicationUser>();
|
||||||
|
setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
|
||||||
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
||||||
|
|
||||||
|
//and a lot of other tables that we haven't defined yet
|
||||||
|
|
||||||
|
// input configurations!!! TODO:Missing
|
||||||
|
|
||||||
|
Console.ForegroundColor = _foregroundColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.WriteLine(e);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await host.StartAsync();
|
|
||||||
Console.WriteLine("Host has started.");
|
|
||||||
await host.WaitForShutdownAsync();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Host failed to start: {ex}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,47 +6,48 @@ using PlanTempus.Database.Core;
|
||||||
|
|
||||||
namespace PlanTempus.SetupInfrastructure
|
namespace PlanTempus.SetupInfrastructure
|
||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
public virtual IConfigurationRoot Configuration()
|
|
||||||
{
|
|
||||||
var configuration = new ConfigurationBuilder()
|
|
||||||
.AddJsonFile("appconfiguration.json")
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
return configuration;
|
public virtual IConfigurationRoot Configuration()
|
||||||
}
|
{
|
||||||
|
var configuration = new ConfigurationBuilder()
|
||||||
|
.AddJsonFile("appconfiguration.json")
|
||||||
|
.Build();
|
||||||
|
|
||||||
public IContainer ConfigureContainer(ConnectionStringTemplateParameters ctp)
|
return configuration;
|
||||||
{
|
}
|
||||||
var builder = new ContainerBuilder();
|
|
||||||
var configuration = Configuration();
|
public IContainer ConfigureContainer()
|
||||||
|
{
|
||||||
|
|
||||||
|
var builder = new ContainerBuilder();
|
||||||
|
var configuration = Configuration();
|
||||||
|
|
||||||
|
|
||||||
builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
|
builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
|
||||||
{
|
{
|
||||||
ConnectionString = configuration.GetConnectionString("DefaultConnection").Replace("{usr}", ctp.user).Replace("{pwd}", ctp.pwd)
|
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.RegisterModule(new TelemetryModule
|
builder.RegisterModule(new TelemetryModule
|
||||||
{
|
{
|
||||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.RegisterModule(new SeqLoggingModule
|
builder.RegisterModule(new SeqLoggingModule
|
||||||
{
|
{
|
||||||
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<Core.Logging.SeqConfiguration>()
|
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<Core.Logging.SeqConfiguration>()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
builder.RegisterAssemblyTypes(typeof(IDbConfigure<>).Assembly)
|
builder.RegisterAssemblyTypes(typeof(IDbConfigure<>).Assembly)
|
||||||
.AsClosedTypesOf(typeof(IDbConfigure<>))
|
.AsClosedTypesOf(typeof(IDbConfigure<>))
|
||||||
.AsSelf();
|
.AsSelf();
|
||||||
|
|
||||||
|
|
||||||
return builder.Build();
|
return builder.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public record ConnectionStringTemplateParameters(string user, string pwd);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id={usr};Password={pwd};"
|
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id={usr};Password={pwd};"
|
||||||
},
|
},
|
||||||
"ApplicationInsights": {
|
"ApplicationInsights": {
|
||||||
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
|
"ConnectionString": "InstrumentationKey=07d2a2b9-5e8e-4924-836e-264f8438f6c5;IngestionEndpoint=https://northeurope-2.in.applicationinsights.azure.com/;LiveEndpoint=https://northeurope.livediagnostics.monitor.azure.com/;ApplicationId=56748c39-2fa3-4880-a1e2-24068e791548",
|
||||||
"UseSeqLoggingTelemetryChannel": true
|
"UseSeqLoggingTelemetryChannel": true
|
||||||
},
|
},
|
||||||
"SeqConfiguration": {
|
"SeqConfiguration": {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id=sathumper;Password=3911;"
|
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id=sathumper;Password=3911;"
|
||||||
},
|
},
|
||||||
"ApplicationInsights": {
|
"ApplicationInsights": {
|
||||||
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
|
"ConnectionString": "InstrumentationKey=07d2a2b9-5e8e-4924-836e-264f8438f6c5;IngestionEndpoint=https://northeurope-2.in.applicationinsights.azure.com/;LiveEndpoint=https://northeurope.livediagnostics.monitor.azure.com/;ApplicationId=56748c39-2fa3-4880-a1e2-24068e791548",
|
||||||
"UseSeqLoggingTelemetryChannel": true
|
"UseSeqLoggingTelemetryChannel": true
|
||||||
},
|
},
|
||||||
"Authentication": "SHA256",
|
"Authentication": "SHA256",
|
||||||
|
|
|
||||||
|
|
@ -1,58 +1,84 @@
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using PlanTempus.Database.Core.Sql;
|
||||||
|
|
||||||
namespace PlanTempus.Tests
|
namespace PlanTempus.Tests
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
public class PostgresTests : TestFixture
|
public class PostgresTests : TestFixture
|
||||||
{
|
{
|
||||||
|
Database.ModuleRegistry.IDbConnectionFactory _connFactory;
|
||||||
|
IDatabaseOperations _databaseOperations;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void MyTestMethod()
|
||||||
|
{
|
||||||
|
_connFactory = Container.Resolve<Database.ModuleRegistry.IDbConnectionFactory>();
|
||||||
|
_databaseOperations = Container.Resolve<IDatabaseOperations>();
|
||||||
|
}
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestDefaultConnection()
|
public void TestDefaultConnection()
|
||||||
{
|
{
|
||||||
var conn = Container.Resolve<IDbConnection>();
|
|
||||||
//https://www.reddit.com/r/dotnet/comments/6wdoyn/how_to_properly_register_dapper_on_net_core_2_di/
|
|
||||||
//https://www.code4it.dev/blog/postgres-crud-dapper/
|
|
||||||
//https://stackoverflow.com/questions/69169247/how-to-create-idbconnection-factory-using-autofac-for-dapper
|
//https://stackoverflow.com/questions/69169247/how-to-create-idbconnection-factory-using-autofac-for-dapper
|
||||||
|
|
||||||
conn.ExecuteSql("SELECT 1 as p");
|
using (var conn = _connFactory.Create())
|
||||||
|
conn.ExecuteSql("SELECT 1 as p");
|
||||||
|
|
||||||
}
|
}
|
||||||
//static HttpClient _client = new HttpClient();
|
|
||||||
//[TestMethod]
|
|
||||||
//public async Task MyTestMethod()
|
|
||||||
//{
|
|
||||||
// _client.BaseAddress = new Uri("http://localhost:5341");
|
|
||||||
|
|
||||||
// var l = new SeqLogger(_client, "", "");
|
|
||||||
|
|
||||||
// for (int i = 0; i < 20; i++)
|
|
||||||
// {
|
|
||||||
|
|
||||||
// await l.LogToSeq(
|
|
||||||
// "Bruger {UserId} loggede ind",
|
|
||||||
// "Debug",
|
|
||||||
// new Dictionary<string, object> { { "UserId", "12345" }, { "Counter", i++ } }
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// var logger = Container.Resolve<Microsoft.ApplicationInsights.TelemetryClient>();
|
|
||||||
|
|
||||||
|
|
||||||
// for (int i = 0; i < 5; i++)
|
|
||||||
// {
|
|
||||||
|
|
||||||
// logger.TrackTrace("Hello 23", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TryOrganizationSetupService()
|
public async Task TestScopeConnectionWithLogging()
|
||||||
{
|
{
|
||||||
var conn = Container.Resolve<IDbConnection>();
|
using var db = _databaseOperations.CreateScope(nameof(TestScopeConnectionWithLogging));
|
||||||
}
|
try
|
||||||
|
{
|
||||||
|
var user = await db.Connection.QuerySqlAsync<string>(
|
||||||
|
"SELECT tablename FROM pg_tables limit 5");
|
||||||
|
|
||||||
|
db.Success();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
db.Error(ex);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestScopeConnectionWithErrorLogging()
|
||||||
|
{
|
||||||
|
using var db = _databaseOperations.CreateScope(nameof(TestScopeConnectionWithLogging));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var user = await db.Connection.QuerySqlAsync<string>(
|
||||||
|
"SELECT tablename FROM pg_tabless limit 5");
|
||||||
|
|
||||||
|
db.Success();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
db.Error(ex);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public async Task TestSimpleDatabaseOperation()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _databaseOperations.ExecuteAsync(async connection =>
|
||||||
|
{
|
||||||
|
return await connection.QuerySqlAsync<string>(
|
||||||
|
"SELECT tablename FROM pg_tables limit 5");
|
||||||
|
}, nameof(TestSimpleDatabaseOperation));
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public async Task TryDbSetup()
|
public async Task TryDbSetup()
|
||||||
{
|
{
|
||||||
|
|
@ -65,6 +91,7 @@ namespace PlanTempus.Tests
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void SetupPostgresql_LISTEN()
|
public void SetupPostgresql_LISTEN()
|
||||||
{
|
{
|
||||||
|
//this is not in use a the moment... kind of premature test to get my mind in to gear
|
||||||
//var builder = new ConfigurationBuilder()
|
//var builder = new ConfigurationBuilder()
|
||||||
// .AddPostgresConfiguration(options =>
|
// .AddPostgresConfiguration(options =>
|
||||||
// {
|
// {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id=sathumper;Password=3911;"
|
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id=sathumper;Password=3911;"
|
||||||
},
|
},
|
||||||
"ApplicationInsights": {
|
"ApplicationInsights": {
|
||||||
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
|
"ConnectionString": "InstrumentationKey=07d2a2b9-5e8e-4924-836e-264f8438f6c5;IngestionEndpoint=https://northeurope-2.in.applicationinsights.azure.com/;LiveEndpoint=https://northeurope.livediagnostics.monitor.azure.com/;ApplicationId=56748c39-2fa3-4880-a1e2-24068e791548",
|
||||||
"UseSeqLoggingTelemetryChannel": true
|
"UseSeqLoggingTelemetryChannel": true
|
||||||
},
|
},
|
||||||
"SeqConfiguration": {
|
"SeqConfiguration": {
|
||||||
|
|
@ -11,4 +11,4 @@
|
||||||
"ApiKey": null,
|
"ApiKey": null,
|
||||||
"Environment": "MSTEST"
|
"Environment": "MSTEST"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue