2025-02-20 00:23:13 +01:00
|
|
|
|
using Microsoft.ApplicationInsights;
|
2025-02-18 16:23:08 +01:00
|
|
|
|
using Microsoft.ApplicationInsights.Channel;
|
|
|
|
|
|
using Microsoft.ApplicationInsights.DataContracts;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2025-02-20 00:23:13 +01:00
|
|
|
|
using PlanTempus.Core.Telemetry;
|
2025-02-18 16:23:08 +01:00
|
|
|
|
|
2025-02-20 00:23:13 +01:00
|
|
|
|
namespace PlanTempus.Core.Logging
|
2025-02-18 16:23:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
public class SeqBackgroundService : BackgroundService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IMessageChannel<ITelemetry> _messageChannel;
|
|
|
|
|
|
private readonly TelemetryClient _telemetryClient;
|
2025-02-20 00:23:13 +01:00
|
|
|
|
private readonly SeqLogger<SeqBackgroundService> _seqLogger;
|
2025-02-18 16:23:08 +01:00
|
|
|
|
|
|
|
|
|
|
public SeqBackgroundService(TelemetryClient telemetryClient,
|
|
|
|
|
|
IMessageChannel<ITelemetry> messageChannel,
|
2025-02-20 00:23:13 +01:00
|
|
|
|
SeqLogger<SeqBackgroundService> seqlogger)
|
2025-02-18 16:23:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
_telemetryClient = telemetryClient;
|
|
|
|
|
|
_messageChannel = messageChannel;
|
|
|
|
|
|
_seqLogger = seqlogger;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
|
|
await foreach (var telemetry in _messageChannel.Reader.ReadAllAsync(stoppingToken))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (telemetry)
|
|
|
|
|
|
{
|
|
|
|
|
|
case ExceptionTelemetry et:
|
|
|
|
|
|
await _seqLogger.LogAsync(et);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case TraceTelemetry et:
|
|
|
|
|
|
await _seqLogger.LogAsync(et);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case DependencyTelemetry et:
|
|
|
|
|
|
await _seqLogger.LogAsync(et);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case RequestTelemetry et:
|
|
|
|
|
|
await _seqLogger.LogAsync(et);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2025-02-20 00:23:13 +01:00
|
|
|
|
case EventTelemetry et:
|
|
|
|
|
|
await _seqLogger.LogAsync(et);
|
|
|
|
|
|
break;
|
2025-02-19 17:53:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
2025-02-20 00:23:13 +01:00
|
|
|
|
default:
|
2025-02-18 16:23:08 +01:00
|
|
|
|
throw new NotSupportedException(telemetry.GetType().Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
//_telemetryClient.TrackException(ex); this is disabled for now, we need to think about the channel structure first
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ex is not OperationCanceledException)
|
|
|
|
|
|
{
|
|
|
|
|
|
_telemetryClient.TrackException(ex);
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_telemetryClient.TrackTrace("Service shutdown started");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
_messageChannel.Dispose();
|
|
|
|
|
|
await base.StopAsync(cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|