This commit is contained in:
Janus Knudsen 2025-03-07 16:17:30 +01:00
parent ddb6abc14e
commit f3ab94eff1
6 changed files with 59 additions and 92 deletions

View file

@ -3,20 +3,12 @@ using Microsoft.ApplicationInsights.Extensibility;
namespace PlanTempus.Core.Telemetry.Enrichers namespace PlanTempus.Core.Telemetry.Enrichers
{ {
public class EnrichWithMetaTelemetry(ITelemetryProcessor next) : ITelemetryProcessor
public class EnrichWithMetaTelemetry : ITelemetryProcessor
{ {
private readonly ITelemetryProcessor _next;
public EnrichWithMetaTelemetry(ITelemetryProcessor next)
{
_next = next;
}
public void Process(ITelemetry item) public void Process(ITelemetry item)
{ {
//nothing going on here yet :) //nothing going on here yet :)
_next.Process(item); next.Process(item);
} }
} }
} }

View file

@ -3,20 +3,12 @@ using Microsoft.ApplicationInsights.Channel;
namespace PlanTempus.Core.Telemetry namespace PlanTempus.Core.Telemetry
{ {
public class SeqTelemetryChannel : InMemoryChannel, ITelemetryChannel public class SeqTelemetryChannel(IMessageChannel<ITelemetry> messageChannel, TelemetryClient telemetryClient)
: InMemoryChannel, ITelemetryChannel
{ {
private readonly IMessageChannel<ITelemetry> _messageChannel;
private readonly TelemetryClient _telemetryClient;
public SeqTelemetryChannel(IMessageChannel<ITelemetry> messageChannel, TelemetryClient telemetryClient)
{
_messageChannel = messageChannel;
_telemetryClient = telemetryClient;
}
public new void Send(ITelemetry telemetry) public new void Send(ITelemetry telemetry)
{ {
if (telemetry.Context.GlobalProperties.TryGetValue("OmitSeqTelemetryChannel", out var value))
if (telemetry.Context.GlobalProperties.TryGetValue("OmitSeqTelemetryChannel", out string value))
if (value == "true") if (value == "true")
{ {
base.Send(telemetry); base.Send(telemetry);
@ -25,7 +17,7 @@ namespace PlanTempus.Core.Telemetry
try try
{ {
var writeTask = _messageChannel.Writer.WriteAsync(telemetry).AsTask(); var writeTask = messageChannel.Writer.WriteAsync(telemetry).AsTask();
writeTask.ContinueWith(t => writeTask.ContinueWith(t =>
{ {
if (t.Exception != null) if (t.Exception != null)
@ -36,8 +28,10 @@ namespace PlanTempus.Core.Telemetry
} }
catch (Exception e) catch (Exception e)
{ {
_telemetryClient.TrackException(e, new Dictionary<string, string> { { "OmitSeqTelemetryChannel", "true" } }); telemetryClient.TrackException(e,
new Dictionary<string, string> { { "OmitSeqTelemetryChannel", "true" } });
} }
base.Send(telemetry); base.Send(telemetry);
} }
} }

View file

@ -22,24 +22,23 @@ namespace PlanTempus.SetupInfrastructure
/// After that is executed it is time for running this main program /// After that is executed it is time for running this main program
/// Remember to use the newly created sathumper /// Remember to use the newly created sathumper
/// "ConnectionStrings": { /// "ConnectionStrings": {
/// "DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=<uid>;Password=<secret>;" /// "DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=[uid];Password=[secret];"
/// </summary> /// </summary>
internal class Program internal class Program
{ {
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
try try
{ {
var host = Host.CreateDefaultBuilder(args) var host = Host.CreateDefaultBuilder(args)
.UseEnvironment("Dev") .UseEnvironment("Dev")
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory()) .UseServiceProviderFactory(
new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>((hostContext, builder) => .ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
{ {
var startup = new Startup(); var startup = new Startup();
startup.ConfigureContainer(builder); startup.ConfigureContainer(builder);
}) })
.Build(); .Build();
await host.StartAsync(); await host.StartAsync();
@ -57,32 +56,24 @@ namespace PlanTempus.SetupInfrastructure
} }
} }
} }
public class ConsoleService
{
public class ConsoleService(
IDbConnectionFactory connectionFactory,
SetupDbAdmin setupDbAdmin,
SetupIdentitySystem setupIdentitySystem,
SetupConfiguration setupConfiguration,
SetupApplicationUser setupApplicationUser)
{
static ConsoleColor _backgroundColor = Console.BackgroundColor; static ConsoleColor _backgroundColor = Console.BackgroundColor;
static ConsoleColor _foregroundColor = Console.ForegroundColor; static ConsoleColor _foregroundColor = Console.ForegroundColor;
private readonly IDbConnectionFactory _connectionFactory;
private readonly SetupDbAdmin _setupDbAdmin;
private readonly SetupIdentitySystem _setupIdentitySystem;
private readonly SetupConfiguration _setupConfiguration;
private readonly SetupApplicationUser _setupApplicationUser;
public ConsoleService(IDbConnectionFactory connectionFactory, SetupDbAdmin setupDbAdmin, SetupIdentitySystem setupIdentitySystem, SetupConfiguration setupConfiguration, SetupApplicationUser setupApplicationUser)
{
_connectionFactory = connectionFactory;
_setupDbAdmin = setupDbAdmin;
_setupIdentitySystem = setupIdentitySystem;
_setupConfiguration = setupConfiguration;
_setupApplicationUser = setupApplicationUser;
}
bool IsSuperAdmin(ConnectionStringParameters parameters) bool IsSuperAdmin(ConnectionStringParameters parameters)
{ {
Console.WriteLine("Testing db access..."); Console.WriteLine("Testing db access...");
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;"; string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
using var conn = _connectionFactory.Create(parameters); using var conn = connectionFactory.Create(parameters);
var result = (dynamic)conn.QuerySql(query).Single(); var result = (dynamic)conn.QuerySql(query).Single();
string username = result.usename; string username = result.usename;
@ -131,9 +122,8 @@ namespace PlanTempus.SetupInfrastructure
Console.Read(); Console.Read();
return false; return false;
} }
static void Welcome() static void Welcome()
{ {
Console.ForegroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.Blue;
@ -151,7 +141,6 @@ namespace PlanTempus.SetupInfrastructure
\/ \/ |__|"); \/ \/ |__|");
Console.WriteLine(); Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
} }
public void Run() public void Run()
@ -165,7 +154,7 @@ namespace PlanTempus.SetupInfrastructure
{ {
Console.WriteLine("Input username:password for a superadmin role"); Console.WriteLine("Input username:password for a superadmin role");
userPass = Console.ReadLine() ?? string.Empty; userPass = Console.ReadLine() ?? string.Empty;
} while (!userPass.Contains(":") || userPass.Split(":").Length != 2 || } while (!userPass.Contains(':') || userPass.Split(":").Length != 2 ||
string.IsNullOrEmpty(userPass.Split(":")[0]) || string.IsNullOrEmpty(userPass.Split(":")[0]) ||
string.IsNullOrEmpty(userPass.Split(":")[1])); string.IsNullOrEmpty(userPass.Split(":")[1]));
@ -182,7 +171,9 @@ namespace PlanTempus.SetupInfrastructure
Console.Write("Database.Core.DCL.SetupDbAdmin..."); Console.Write("Database.Core.DCL.SetupDbAdmin...");
sw.Start(); sw.Start();
_setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" }, superUser); setupDbAdmin.With(
new SetupDbAdmin.Command
{ Password = "3911", Schema = "system", User = "heimdall" }, superUser);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms"); Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::"); Console.WriteLine("::");
@ -193,14 +184,14 @@ namespace PlanTempus.SetupInfrastructure
//use application user, we use that role from now. //use application user, we use that role from now.
var connParams = new ConnectionStringParameters(User: "heimdall", Pwd: "3911"); var connParams = new ConnectionStringParameters(User: "heimdall", Pwd: "3911");
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" }, connParams); setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" }, connParams);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms"); Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::"); Console.WriteLine("::");
Console.WriteLine("::"); Console.WriteLine("::");
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration..."); Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
sw.Restart(); sw.Restart();
_setupConfiguration.With(new SetupConfiguration.Command(), connParams); setupConfiguration.With(new SetupConfiguration.Command(), connParams);
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms"); Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
@ -209,7 +200,9 @@ namespace PlanTempus.SetupInfrastructure
Console.Write("Database.Core.DCL.SetupApplicationUser..."); Console.Write("Database.Core.DCL.SetupApplicationUser...");
sw.Start(); sw.Start();
_setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" }, superUser); setupApplicationUser.With(
new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" },
superUser);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms"); Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
@ -219,7 +212,6 @@ namespace PlanTempus.SetupInfrastructure
Console.ForegroundColor = _foregroundColor; Console.ForegroundColor = _foregroundColor;
} }
} }
catch (Exception e) catch (Exception e)
@ -227,12 +219,7 @@ namespace PlanTempus.SetupInfrastructure
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(); Console.WriteLine();
Console.WriteLine(e); Console.WriteLine(e);
}
} }
} }
} }
}

View file

@ -46,18 +46,6 @@ public abstract class TestFixture
protected virtual void CreateContainerBuilder() protected virtual void CreateContainerBuilder()
{ {
var configuration = Configuration(); var configuration = Configuration();
//var logger = new LoggerConfiguration()
// .MinimumLevel.Verbose()
// .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
// .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error)
// .WriteTo.Seq("http://localhost:5341", apiKey: "Gt8hS9ClGNfOCAdswDlW")
// .WriteTo.ApplicationInsights(configuration.Get<string>("ApplicationInsights:ConnectionString"),
// TelemetryConverter.Traces)
// .Enrich.FromLogContext()
// .Enrich.WithMachineName()
// .CreateLogger();
var builder = new ContainerBuilder(); var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(Logger<>)) builder.RegisterGeneric(typeof(Logger<>))
@ -90,10 +78,9 @@ public abstract class TestFixture
var telemetryClient = Container.Resolve<TelemetryClient>(); var telemetryClient = Container.Resolve<TelemetryClient>();
telemetryClient.Flush(); telemetryClient.Flush();
if (Container != null) if (Container is null) return;
{
Container.Dispose(); Container.Dispose();
Container = null; Container = null;
} }
} }
}

7
global.json Normal file
View file

@ -0,0 +1,7 @@
{
"sdk": {
"version": "9.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
}