2025-02-11 18:46:51 +01:00
using Microsoft.ApplicationInsights ;
using Microsoft.Extensions.Hosting ;
namespace Core.Telemetry
{
public class SeqBackgroundService : BackgroundService
{
2025-02-13 00:40:42 +01:00
private readonly IMessageChannel _messageChannel ;
2025-02-11 18:46:51 +01:00
private readonly TelemetryClient _telemetryClient ;
2025-02-13 00:40:42 +01:00
private readonly HttpClient _httpClient ;
2025-02-11 18:46:51 +01:00
2025-02-13 00:40:42 +01:00
public SeqBackgroundService (
TelemetryClient telemetryClient ,
IMessageChannel messageChannel ,
HttpClient httpClient )
2025-02-11 18:46:51 +01:00
{
_telemetryClient = telemetryClient ;
2025-02-13 00:40:42 +01:00
_messageChannel = messageChannel ;
_httpClient = httpClient ;
2025-02-11 18:46:51 +01:00
}
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
{
2025-02-13 00:40:42 +01:00
try
2025-02-11 18:46:51 +01:00
{
2025-02-13 00:40:42 +01:00
while ( ! stoppingToken . IsCancellationRequested )
await foreach ( var message in _messageChannel . Reader . ReadAllAsync ( stoppingToken ) )
2025-02-11 18:46:51 +01:00
{
2025-02-13 00:40:42 +01:00
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 ) ;
}
2025-02-11 18:46:51 +01:00
}
2025-02-13 00:40:42 +01:00
}
catch ( Exception ex )
{
if ( ex is not OperationCanceledException )
2025-02-11 18:46:51 +01:00
{
_telemetryClient . TrackException ( ex ) ;
2025-02-13 00:40:42 +01:00
throw ;
2025-02-11 18:46:51 +01:00
}
2025-02-13 00:40:42 +01:00
_telemetryClient . TrackTrace ( "Service shutdown påbegyndt" ) ;
2025-02-11 18:46:51 +01:00
}
}
public override async Task StopAsync ( CancellationToken cancellationToken )
{
2025-02-13 00:40:42 +01:00
_messageChannel . Dispose ( ) ;
2025-02-11 18:46:51 +01:00
await base . StopAsync ( cancellationToken ) ;
}
}
}