This is near the end of this Seq Logging Implementation

This commit is contained in:
Janus C. H. Knudsen 2025-02-22 20:14:56 +01:00
parent 78d49a9829
commit 099f6467d2
9 changed files with 212 additions and 123 deletions

View file

@ -19,6 +19,9 @@ namespace PlanTempus.Core.Logging
_telemetryClient = telemetryClient;
_messageChannel = messageChannel;
_seqLogger = seqlogger;
_telemetryClient.TrackTrace("SeqBackgroundService started");
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@ -72,12 +75,12 @@ namespace PlanTempus.Core.Logging
throw;
}
_telemetryClient.TrackTrace("Service shutdown started");
}
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_telemetryClient.TrackTrace("StopAsync called: Service shutdown started");
_messageChannel.Dispose();
await base.StopAsync(cancellationToken);
}

View file

@ -1,32 +1,31 @@
using Autofac;
using PlanTempus.Core.Logging;
using PlanTempus.Core.Telemetry;
namespace PlanTempus.Core.ModuleRegistry
{
public class SeqLoggingModule : Module
{
public required SeqConfiguration SeqConfiguration { get; set; }
public class SeqLoggingModule : Module
{
public required SeqConfiguration SeqConfiguration { get; set; }
protected override void Load(ContainerBuilder builder)
{
protected override void Load(ContainerBuilder builder)
{
//builder.RegisterType<MessageChannel>()
// .As<IMessageChannel<Microsoft.ApplicationInsights.Channel.ITelemetry>>()
// .SingleInstance();
//builder.RegisterType<MessageChannel>()
// .As<IMessageChannel<Microsoft.ApplicationInsights.Channel.ITelemetry>>()
// .SingleInstance();
builder.RegisterType<SeqBackgroundService>()
.As<Microsoft.Extensions.Hosting.IHostedService>()
.SingleInstance();
builder.RegisterType<SeqBackgroundService>()
//.As<Microsoft.Extensions.Hosting.IHostedService>()
.SingleInstance();
builder.RegisterGeneric(typeof(SeqLogger<>));
builder.RegisterGeneric(typeof(SeqLogger<>));
builder.RegisterInstance(SeqConfiguration);
builder.RegisterInstance(SeqConfiguration);
builder.RegisterType<SeqHttpClient>()
.As<SeqHttpClient>()
.SingleInstance();
builder.RegisterType<SeqHttpClient>()
.As<SeqHttpClient>()
.SingleInstance();
}
}
}
}
}

View file

@ -15,6 +15,14 @@ namespace PlanTempus.Core.ModuleRegistry
configuration.ConnectionString = TelemetryConfig.ConnectionString;
configuration.TelemetryChannel.DeveloperMode = true;
var client = new Microsoft.ApplicationInsights.TelemetryClient(configuration);
client.Context.GlobalProperties["Application"] = GetType().Namespace.Split('.')[0];
client.Context.GlobalProperties["MachineName"] = Environment.MachineName;
client.Context.GlobalProperties["Version"] = Environment.Version.ToString();
client.Context.GlobalProperties["ProcessorCount"] = Environment.ProcessorCount.ToString();
builder.Register(c => client).InstancePerLifetimeScope();
if (TelemetryConfig.UseSeqLoggingTelemetryChannel)
{
var messageChannel = new Telemetry.MessageChannel();
@ -23,21 +31,13 @@ namespace PlanTempus.Core.ModuleRegistry
.As<Telemetry.IMessageChannel<ITelemetry>>()
.SingleInstance();
configuration.TelemetryChannel = new Telemetry.SeqLoggingTelemetryChannel(messageChannel);
configuration.TelemetryChannel = new Telemetry.SeqTelemetryChannel(messageChannel, client);
}
var telemetryProcessorChain = new Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryProcessorChainBuilder(configuration);
telemetryProcessorChain.Use(next => new Telemetry.Enrichers.EnrichWithMetaTelemetry(next));
telemetryProcessorChain.Build();
var client = new Microsoft.ApplicationInsights.TelemetryClient(configuration);
client.Context.GlobalProperties["Application"] = GetType().Namespace.Split('.')[0];
client.Context.GlobalProperties["MachineName"] = Environment.MachineName;
client.Context.GlobalProperties["Version"] = Environment.Version.ToString();
client.Context.GlobalProperties["ProcessorCount"] = Environment.ProcessorCount.ToString();
builder.Register(c => client).InstancePerLifetimeScope();
}
}

View file

@ -1,27 +0,0 @@
using Microsoft.ApplicationInsights.Channel;
namespace PlanTempus.Core.Telemetry
{
public class SeqLoggingTelemetryChannel : InMemoryChannel, ITelemetryChannel
{
private readonly IMessageChannel<ITelemetry> _messageChannel;
public SeqLoggingTelemetryChannel(IMessageChannel<ITelemetry> messageChannel)
{
_messageChannel = messageChannel;
}
public new void Send(ITelemetry telemetry)
{
var writeTask = _messageChannel.Writer.WriteAsync(telemetry).AsTask();
writeTask.ContinueWith(t =>
{
if (t.Exception != null)
{
throw t.Exception;
}
}, TaskContinuationOptions.OnlyOnFaulted);
base.Send(telemetry);
}
}
}

View file

@ -0,0 +1,37 @@
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
namespace PlanTempus.Core.Telemetry
{
public class SeqTelemetryChannel : 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)
{
if (telemetry.Context.GlobalProperties["OmitSeqTelemetryChannel"] != "true")
try
{
var writeTask = _messageChannel.Writer.WriteAsync(telemetry).AsTask();
writeTask.ContinueWith(t =>
{
if (t.Exception != null)
{
throw t.Exception;
}
}, TaskContinuationOptions.OnlyOnFaulted);
}
catch (Exception e)
{
_telemetryClient.TrackException(e, new Dictionary<string, string> { { "OmitSeqTelemetryChannel", "true" } });
}
base.Send(telemetry);
}
}
}