PlanTempusApp/Core/Telemetry/SeqBackgroundService.cs

64 lines
2.2 KiB
C#

using Microsoft.ApplicationInsights;
using Microsoft.Extensions.Hosting;
namespace Core.Telemetry
{
public class SeqBackgroundService : BackgroundService
{
private readonly IMessageChannel _messageChannel;
private readonly TelemetryClient _telemetryClient;
private readonly HttpClient _httpClient;
public SeqBackgroundService(
TelemetryClient telemetryClient,
IMessageChannel messageChannel,
HttpClient httpClient)
{
_telemetryClient = telemetryClient;
_messageChannel = messageChannel;
_httpClient = httpClient;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
while (!stoppingToken.IsCancellationRequested)
await foreach (var message in _messageChannel.Reader.ReadAllAsync(stoppingToken))
{
try
{
//using var response = await _httpClient.SendAsync(message, stoppingToken);
//if (!response.IsSuccessStatusCode)
//{
// _telemetryClient.TrackTrace($"HTTP kald fejlede med status {response.StatusCode}", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Warning);
// continue;
//}
}
catch (Exception ex)
{
_telemetryClient.TrackException(ex);
}
}
}
catch (Exception ex)
{
if (ex is not OperationCanceledException)
{
_telemetryClient.TrackException(ex);
throw;
}
_telemetryClient.TrackTrace("Service shutdown påbegyndt");
}
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_messageChannel.Dispose();
await base.StopAsync(cancellationToken);
}
}
}