Working on BackgroundService

This commit is contained in:
Janus Knudsen 2025-02-20 17:14:53 +01:00
parent 9f4996bc8f
commit ad4ed12f00
12 changed files with 368 additions and 317 deletions

View file

@ -1,10 +1,14 @@
{
"AllowedHosts": "*",
"ConnectionStrings": {
"ptdb": "Host=localhost;Port=5433;Database=ptdb01;User Id=sathumper;Password=3911;"
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id={usr};Password={pwd};"
},
"TelemetryConfig": {
"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",
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
"UseSeqLoggingTelemetryChannel": true
},
"SeqConfiguration": {
"IngestionEndpoint": "http://localhost:5341",
"ApiKey": null,
"Environment": "MSTEST"
}
}

View file

@ -19,7 +19,6 @@ namespace PlanTempus.Core.Logging
_telemetryClient = telemetryClient;
_messageChannel = messageChannel;
_seqLogger = seqlogger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)

View file

@ -10,9 +10,10 @@ namespace PlanTempus.Core.Logging
private readonly string _machineName;
private readonly SeqConfiguration _configuration;
public SeqLogger(SeqHttpClient httpClient, string environmentName, SeqConfiguration configuration)
public SeqLogger(SeqHttpClient httpClient, SeqConfiguration configuration)
{
_httpClient = httpClient;
_configuration = configuration;
}
public async Task LogAsync(TraceTelemetry trace, CancellationToken cancellationToken = default)
@ -153,7 +154,7 @@ namespace PlanTempus.Core.Logging
seqEvent.Add($"prop_{prop.Key}", prop.Value);
foreach (var prop in req.Context.GlobalProperties)
seqEvent.Add($"global_{prop.Key}", prop.Value);
seqEvent.Add($"{prop.Key}", prop.Value);
await SendToSeqAsync(seqEvent, cancellationToken);
}
@ -174,7 +175,7 @@ namespace PlanTempus.Core.Logging
result.EnsureSuccessStatusCode();
}
private string MapSeverityToLevel(SeverityLevel? severity)
private static string MapSeverityToLevel(SeverityLevel? severity)
{
return severity switch
{
@ -186,7 +187,7 @@ namespace PlanTempus.Core.Logging
_ => "Information"
};
}
private string FormatExceptionForSeq(Exception ex)
private static string FormatExceptionForSeq(Exception ex)
{
var sb = new StringBuilder();
var exceptionCount = 0;

View file

@ -6,6 +6,8 @@ namespace PlanTempus.Core.ModuleRegistry
{
public class SeqLoggingModule : Module
{
public required SeqConfiguration SeqConfiguration { get; set; }
protected override void Load(ContainerBuilder builder)
{
@ -17,12 +19,14 @@ namespace PlanTempus.Core.ModuleRegistry
.As<Microsoft.Extensions.Hosting.IHostedService>()
.SingleInstance();
builder.RegisterGeneric(typeof(SeqLogger<>));
builder.RegisterInstance(SeqConfiguration);
builder.RegisterType<SeqHttpClient>()
.As<SeqHttpClient>()
.SingleInstance();
}
}
}

View file

@ -6,6 +6,10 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Database\PlanTempus.Database.csproj" />
</ItemGroup>

View file

@ -1,5 +1,6 @@
using Autofac;
using Insight.Database;
using Microsoft.Extensions.Hosting;
using PlanTempus.Database.ConfigurationManagementSystem;
using PlanTempus.Database.Core.DCL;
using PlanTempus.Database.Core.DDL;
@ -189,5 +190,35 @@ namespace PlanTempus.SetupInfrastructure
Console.ForegroundColor = ConsoleColor.White;
}
async Task Run(string[] args)
{
try
{
var host = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
{
var startup = new Startup();
var connectionStringParams = new Startup.ConnectionStringTemplateParameters("your_user", "your_password");
startup.ConfigureContainer(connectionStringParams);
})
.ConfigureServices((hostContext, services) =>
{
// Konfigurer andre services her (hvis nødvendigt)
})
.Build();
await host.StartAsync();
Console.WriteLine("Host has started.");
await host.WaitForShutdownAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Host failed to start: {ex}");
}
}
}
}

View file

@ -33,6 +33,12 @@ namespace PlanTempus.SetupInfrastructure
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
});
builder.RegisterModule(new SeqLoggingModule
{
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<Core.Logging.SeqConfiguration>()
});
builder.RegisterAssemblyTypes(typeof(IDbConfigure<>).Assembly)
.AsClosedTypesOf(typeof(IDbConfigure<>))
.AsSelf();

View file

@ -1,9 +1,14 @@
{
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id={usr};Password={pwd};"
},
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/"
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
"UseSeqLoggingTelemetryChannel": true
},
"SeqConfiguration": {
"IngestionEndpoint": "http://localhost:5341",
"ApiKey": null,
"Environment": "MSTEST"
}
}

View file

@ -23,7 +23,7 @@ namespace PlanTempus.Tests.Logging
var config = new SeqConfiguration("http://localhost:5341", null, "MSTEST");
var httpClient = new SeqHttpClient(config);
var logger = new SeqLogger<SeqBackgroundService>(httpClient, Environment.MachineName, config);
var logger = new SeqLogger<SeqBackgroundService>(httpClient, config);
_service = new SeqBackgroundService(telemetryClient, _messageChannel, logger);
_cts = new CancellationTokenSource();

View file

@ -17,7 +17,7 @@ namespace PlanTempus.Tests.Logging
_testId = Guid.NewGuid().ToString();
var config = new SeqConfiguration("http://localhost:5341", null, "MSTEST");
_httpClient = new SeqHttpClient(config);
_logger = new SeqLogger<SeqLoggerTests>(_httpClient, Environment.MachineName, config);
_logger = new SeqLogger<SeqLoggerTests>(_httpClient, config);
}
[TestMethod]

View file

@ -52,16 +52,8 @@ namespace PlanTempus.Tests
// .Enrich.WithMachineName()
// .CreateLogger();
//Log.Logger = logger;
//Log.Logger.Verbose("Is thos work");
var builder = new ContainerBuilder();
//builder.Register(c => new SerilogLoggerFactory(logger))
// .As<ILoggerFactory>()
// .SingleInstance();
builder.RegisterGeneric(typeof(Logger<>))
.As(typeof(ILogger<>))
.SingleInstance();

View file

@ -5,5 +5,10 @@
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
"UseSeqLoggingTelemetryChannel": true
},
"SeqConfiguration": {
"IngestionEndpoint": "http://localhost:5341",
"ApiKey": null,
"Environment": "MSTEST"
}
}