Working on BackgroundService for Seq Logging
This commit is contained in:
parent
48578b216f
commit
7bcb7b0e66
9 changed files with 240 additions and 53 deletions
9
Core/Configurations/IAppConfiguration.cs
Normal file
9
Core/Configurations/IAppConfiguration.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
namespace Core.Configurations
|
||||
{
|
||||
/// <summary>
|
||||
/// Marker interface for application configurations that should be automatically registered in the DI container.
|
||||
/// Classes implementing this interface will be loaded from configuration and registered as singletons.
|
||||
/// </summary>
|
||||
public interface IAppConfiguration { }
|
||||
|
||||
}
|
||||
20
Core/ModuleRegistry/SeqBackgroundServiceModule.cs
Normal file
20
Core/ModuleRegistry/SeqBackgroundServiceModule.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using Autofac;
|
||||
using Core.Telemetry;
|
||||
|
||||
namespace Core.ModuleRegistry
|
||||
{
|
||||
public class SeqBackgroundServiceModule : Module
|
||||
{
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
|
||||
builder.RegisterType<MessageChannel>()
|
||||
.As<IMessageChannel>()
|
||||
.SingleInstance();
|
||||
|
||||
builder.RegisterType<SeqBackgroundService>()
|
||||
.As<Microsoft.Extensions.Hosting.IHostedService>()
|
||||
.SingleInstance();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Core/Telemetry/IMessageChannel.cs
Normal file
9
Core/Telemetry/IMessageChannel.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using System.Threading.Channels;
|
||||
namespace Core.Telemetry
|
||||
{
|
||||
public interface IMessageChannel : IDisposable
|
||||
{
|
||||
ChannelWriter<HttpRequestMessage> Writer { get; }
|
||||
ChannelReader<HttpRequestMessage> Reader { get; }
|
||||
}
|
||||
}
|
||||
22
Core/Telemetry/MessageChannel.cs
Normal file
22
Core/Telemetry/MessageChannel.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using System.Threading.Channels;
|
||||
|
||||
namespace Core.Telemetry
|
||||
{
|
||||
public class MessageChannel : IMessageChannel
|
||||
{
|
||||
private readonly Channel<HttpRequestMessage> _channel;
|
||||
|
||||
public MessageChannel()
|
||||
{
|
||||
_channel = Channel.CreateUnbounded<HttpRequestMessage>();
|
||||
}
|
||||
|
||||
public ChannelWriter<HttpRequestMessage> Writer => _channel.Writer;
|
||||
public ChannelReader<HttpRequestMessage> Reader => _channel.Reader;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_channel.Writer.Complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,55 +1,63 @@
|
|||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Channels;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Core.Telemetry
|
||||
{
|
||||
public class SeqBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly Channel<string> _channel;
|
||||
private readonly HttpClient _client;
|
||||
private readonly string _url;
|
||||
private readonly IMessageChannel _messageChannel;
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public SeqBackgroundService(TelemetryClient telemetryClient, Configurations.IConfiguration configuration)
|
||||
public SeqBackgroundService(
|
||||
TelemetryClient telemetryClient,
|
||||
IMessageChannel messageChannel,
|
||||
HttpClient httpClient)
|
||||
{
|
||||
_telemetryClient = telemetryClient;
|
||||
_url = configuration["Seq:Url"]; // eller hvor din Seq URL kommer fra
|
||||
_client = new HttpClient();
|
||||
_channel = Channel.CreateUnbounded<string>();
|
||||
}
|
||||
|
||||
public void EnqueueMessage(string message)
|
||||
{
|
||||
_channel.Writer.TryWrite(message);
|
||||
_messageChannel = messageChannel;
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
await foreach (var message in _channel.Reader.ReadAllAsync(stoppingToken))
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
await foreach (var message in _messageChannel.Reader.ReadAllAsync(stoppingToken))
|
||||
{
|
||||
await _client.PostAsync(_url, new StringContent(message), 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)
|
||||
}
|
||||
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)
|
||||
{
|
||||
_channel.Writer.Complete();
|
||||
|
||||
_messageChannel.Dispose();
|
||||
await base.StopAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue