Working on BackgroundService
This commit is contained in:
parent
9f4996bc8f
commit
ad4ed12f00
12 changed files with 368 additions and 317 deletions
|
|
@ -1,10 +1,14 @@
|
||||||
{
|
{
|
||||||
"AllowedHosts": "*",
|
|
||||||
"ConnectionStrings": {
|
"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": {
|
"ApplicationInsights": {
|
||||||
"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",
|
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
|
||||||
"UseSeqLoggingTelemetryChannel": true
|
"UseSeqLoggingTelemetryChannel": true
|
||||||
|
},
|
||||||
|
"SeqConfiguration": {
|
||||||
|
"IngestionEndpoint": "http://localhost:5341",
|
||||||
|
"ApiKey": null,
|
||||||
|
"Environment": "MSTEST"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -19,7 +19,6 @@ namespace PlanTempus.Core.Logging
|
||||||
_telemetryClient = telemetryClient;
|
_telemetryClient = telemetryClient;
|
||||||
_messageChannel = messageChannel;
|
_messageChannel = messageChannel;
|
||||||
_seqLogger = seqlogger;
|
_seqLogger = seqlogger;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,10 @@ namespace PlanTempus.Core.Logging
|
||||||
private readonly string _machineName;
|
private readonly string _machineName;
|
||||||
private readonly SeqConfiguration _configuration;
|
private readonly SeqConfiguration _configuration;
|
||||||
|
|
||||||
public SeqLogger(SeqHttpClient httpClient, string environmentName, SeqConfiguration configuration)
|
public SeqLogger(SeqHttpClient httpClient, SeqConfiguration configuration)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task LogAsync(TraceTelemetry trace, CancellationToken cancellationToken = default)
|
public async Task LogAsync(TraceTelemetry trace, CancellationToken cancellationToken = default)
|
||||||
|
|
@ -153,7 +154,7 @@ namespace PlanTempus.Core.Logging
|
||||||
seqEvent.Add($"prop_{prop.Key}", prop.Value);
|
seqEvent.Add($"prop_{prop.Key}", prop.Value);
|
||||||
|
|
||||||
foreach (var prop in req.Context.GlobalProperties)
|
foreach (var prop in req.Context.GlobalProperties)
|
||||||
seqEvent.Add($"global_{prop.Key}", prop.Value);
|
seqEvent.Add($"{prop.Key}", prop.Value);
|
||||||
|
|
||||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
@ -174,7 +175,7 @@ namespace PlanTempus.Core.Logging
|
||||||
result.EnsureSuccessStatusCode();
|
result.EnsureSuccessStatusCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string MapSeverityToLevel(SeverityLevel? severity)
|
private static string MapSeverityToLevel(SeverityLevel? severity)
|
||||||
{
|
{
|
||||||
return severity switch
|
return severity switch
|
||||||
{
|
{
|
||||||
|
|
@ -186,7 +187,7 @@ namespace PlanTempus.Core.Logging
|
||||||
_ => "Information"
|
_ => "Information"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
private string FormatExceptionForSeq(Exception ex)
|
private static string FormatExceptionForSeq(Exception ex)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
var exceptionCount = 0;
|
var exceptionCount = 0;
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ namespace PlanTempus.Core.ModuleRegistry
|
||||||
{
|
{
|
||||||
public class SeqLoggingModule : Module
|
public class SeqLoggingModule : Module
|
||||||
{
|
{
|
||||||
|
public required SeqConfiguration SeqConfiguration { get; set; }
|
||||||
|
|
||||||
protected override void Load(ContainerBuilder builder)
|
protected override void Load(ContainerBuilder builder)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -17,12 +19,14 @@ namespace PlanTempus.Core.ModuleRegistry
|
||||||
.As<Microsoft.Extensions.Hosting.IHostedService>()
|
.As<Microsoft.Extensions.Hosting.IHostedService>()
|
||||||
.SingleInstance();
|
.SingleInstance();
|
||||||
|
|
||||||
|
builder.RegisterGeneric(typeof(SeqLogger<>));
|
||||||
|
|
||||||
|
builder.RegisterInstance(SeqConfiguration);
|
||||||
|
|
||||||
builder.RegisterType<SeqHttpClient>()
|
builder.RegisterType<SeqHttpClient>()
|
||||||
.As<SeqHttpClient>()
|
.As<SeqHttpClient>()
|
||||||
.SingleInstance();
|
.SingleInstance();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Database\PlanTempus.Database.csproj" />
|
<ProjectReference Include="..\Database\PlanTempus.Database.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using PlanTempus.Database.ConfigurationManagementSystem;
|
using PlanTempus.Database.ConfigurationManagementSystem;
|
||||||
using PlanTempus.Database.Core.DCL;
|
using PlanTempus.Database.Core.DCL;
|
||||||
using PlanTempus.Database.Core.DDL;
|
using PlanTempus.Database.Core.DDL;
|
||||||
|
|
@ -189,5 +190,35 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,12 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
builder.RegisterModule(new SeqLoggingModule
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,14 @@
|
||||||
{
|
{
|
||||||
"AllowedHosts": "*",
|
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"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=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
|
||||||
|
"UseSeqLoggingTelemetryChannel": true
|
||||||
|
},
|
||||||
|
"SeqConfiguration": {
|
||||||
|
"IngestionEndpoint": "http://localhost:5341",
|
||||||
|
"ApiKey": null,
|
||||||
|
"Environment": "MSTEST"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -23,7 +23,7 @@ namespace PlanTempus.Tests.Logging
|
||||||
var config = new SeqConfiguration("http://localhost:5341", null, "MSTEST");
|
var config = new SeqConfiguration("http://localhost:5341", null, "MSTEST");
|
||||||
|
|
||||||
var httpClient = new SeqHttpClient(config);
|
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);
|
_service = new SeqBackgroundService(telemetryClient, _messageChannel, logger);
|
||||||
_cts = new CancellationTokenSource();
|
_cts = new CancellationTokenSource();
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ namespace PlanTempus.Tests.Logging
|
||||||
_testId = Guid.NewGuid().ToString();
|
_testId = Guid.NewGuid().ToString();
|
||||||
var config = new SeqConfiguration("http://localhost:5341", null, "MSTEST");
|
var config = new SeqConfiguration("http://localhost:5341", null, "MSTEST");
|
||||||
_httpClient = new SeqHttpClient(config);
|
_httpClient = new SeqHttpClient(config);
|
||||||
_logger = new SeqLogger<SeqLoggerTests>(_httpClient, Environment.MachineName, config);
|
_logger = new SeqLogger<SeqLoggerTests>(_httpClient, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|
|
||||||
|
|
@ -52,16 +52,8 @@ namespace PlanTempus.Tests
|
||||||
// .Enrich.WithMachineName()
|
// .Enrich.WithMachineName()
|
||||||
// .CreateLogger();
|
// .CreateLogger();
|
||||||
|
|
||||||
//Log.Logger = logger;
|
|
||||||
|
|
||||||
//Log.Logger.Verbose("Is thos work");
|
|
||||||
var builder = new ContainerBuilder();
|
var builder = new ContainerBuilder();
|
||||||
|
|
||||||
|
|
||||||
//builder.Register(c => new SerilogLoggerFactory(logger))
|
|
||||||
// .As<ILoggerFactory>()
|
|
||||||
// .SingleInstance();
|
|
||||||
|
|
||||||
builder.RegisterGeneric(typeof(Logger<>))
|
builder.RegisterGeneric(typeof(Logger<>))
|
||||||
.As(typeof(ILogger<>))
|
.As(typeof(ILogger<>))
|
||||||
.SingleInstance();
|
.SingleInstance();
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,10 @@
|
||||||
"ApplicationInsights": {
|
"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
|
"UseSeqLoggingTelemetryChannel": true
|
||||||
|
},
|
||||||
|
"SeqConfiguration": {
|
||||||
|
"IngestionEndpoint": "http://localhost:5341",
|
||||||
|
"ApiKey": null,
|
||||||
|
"Environment": "MSTEST"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue