MessageChannel work for Seq Logging

This commit is contained in:
Janus Knudsen 2025-02-14 17:45:49 +01:00
parent e777135d62
commit bf50563ab7
6 changed files with 119 additions and 84 deletions

View file

@ -21,6 +21,7 @@
<ItemGroup>
<Folder Include="Configurations\AzureAppConfigurationProvider\" />
<Folder Include="Configurations\PostgresqlConfigurationBuilder\" />
<Folder Include="Logging\" />
</ItemGroup>
</Project>

View file

@ -9,7 +9,7 @@ namespace Core.ModuleRegistry
{
builder.RegisterType<MessageChannel>()
.As<IMessageChannel>()
.As<IMessageChannel<Microsoft.ApplicationInsights.Channel.ITelemetry>>()
.SingleInstance();
builder.RegisterType<SeqBackgroundService>()

View file

@ -1,9 +1,9 @@
using System.Threading.Channels;
namespace Core.Telemetry
{
public interface IMessageChannel : IDisposable
public interface IMessageChannel<T> : IDisposable
{
ChannelWriter<HttpRequestMessage> Writer { get; }
ChannelReader<HttpRequestMessage> Reader { get; }
ChannelWriter<T> Writer { get; }
ChannelReader<T> Reader { get; }
}
}

View file

@ -1,18 +1,19 @@
using System.Threading.Channels;
using Microsoft.ApplicationInsights.Channel;
using System.Threading.Channels;
namespace Core.Telemetry
{
public class MessageChannel : IMessageChannel
public class MessageChannel : IMessageChannel<ITelemetry>
{
private readonly Channel<HttpRequestMessage> _channel;
private readonly Channel<ITelemetry> _channel;
public MessageChannel()
{
_channel = Channel.CreateUnbounded<HttpRequestMessage>();
_channel = Channel.CreateUnbounded<ITelemetry>();
}
public ChannelWriter<HttpRequestMessage> Writer => _channel.Writer;
public ChannelReader<HttpRequestMessage> Reader => _channel.Reader;
public ChannelWriter<ITelemetry> Writer => _channel.Writer;
public ChannelReader<ITelemetry> Reader => _channel.Reader;
public void Dispose()
{

View file

@ -1,22 +1,35 @@
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.Extensions.Hosting;
using System.Net.Http.Headers;
using System.Text;
namespace Core.Telemetry
{
public class SeqBackgroundService : BackgroundService
{
private readonly IMessageChannel _messageChannel;
private readonly IMessageChannel<ITelemetry> _messageChannel;
private readonly TelemetryClient _telemetryClient;
private readonly HttpClient _httpClient;
public SeqBackgroundService(
TelemetryClient telemetryClient,
IMessageChannel messageChannel,
public SeqBackgroundService(TelemetryClient telemetryClient,
IMessageChannel<ITelemetry> messageChannel,
HttpClient httpClient)
{
_telemetryClient = telemetryClient;
_messageChannel = messageChannel;
_httpClient = httpClient;
_httpClient = new HttpClient()
{
BaseAddress = new Uri("http://localhost:5341"),
Timeout = TimeSpan.FromSeconds(30)
};
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@ -29,7 +42,32 @@ namespace Core.Telemetry
try
{
//using var response = await _httpClient.SendAsync(message, stoppingToken);
var eventTelemetry = message as Microsoft.ApplicationInsights.DataContracts.EventTelemetry;
var level = "Information";
var seqEvent = new Dictionary<string, object>
{
{ "@t", DateTime.UtcNow.ToString("o") },
{ "@mt", eventTelemetry.Name },
{ "@l", level } // "Information", "Warning", "Error", etc.
};
foreach (var prop in eventTelemetry.Context.GlobalProperties)
{
seqEvent.Add(prop.Key, prop.Value);
}
var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(seqEvent), Encoding.UTF8, "application/vnd.serilog.clef");
var key = "4XhWFtY4jJ0NBgohBAFF"; ;
//Gt8hS9ClGNfOCAdswDlW
var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"/ingest/clef?apiKey={key}");
requestMessage.Content = content;
var response = await _httpClient.SendAsync(requestMessage, stoppingToken);
response.EnsureSuccessStatusCode();
//if (!response.IsSuccessStatusCode)
//{
// _telemetryClient.TrackTrace($"HTTP kald fejlede med status {response.StatusCode}", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Warning);
@ -38,7 +76,7 @@ namespace Core.Telemetry
}
catch (Exception ex)
{
_telemetryClient.TrackException(ex);
//_telemetryClient.TrackException(ex); this is disabled for now, we need to think about the channel structure first
}
}
}
@ -50,13 +88,12 @@ namespace Core.Telemetry
throw;
}
_telemetryClient.TrackTrace("Service shutdown påbegyndt");
_telemetryClient.TrackTrace("Service shutdown started");
}
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_messageChannel.Dispose();
await base.StopAsync(cancellationToken);
}

View file

@ -5,13 +5,15 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Logging;
using Core.Telemetry;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
namespace Tests
{
[TestClass]
public class MessageChannelIntegrationTests : TestFixture
{
private IMessageChannel _messageChannel;
private IMessageChannel<ITelemetry> _messageChannel;
private SeqBackgroundService _service;
private CancellationTokenSource _cts;
@ -28,31 +30,25 @@ namespace Tests
[TestMethod]
public async Task Messages_ShouldBeProcessedFromQueue()
{
// Arrange
var processedMessages = new List<HttpRequestMessage>();
// Start service
var serviceTask = _service.StartAsync(_cts.Token);
// Act
// Send nogle beskeder til køen
for (int i = 0; i < 5; i++)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"http://test.com/{i}");
await _messageChannel.Writer.WriteAsync(message);
var eventTelemetry = new EventTelemetry("SomeEvent");
await _messageChannel.Writer.WriteAsync(eventTelemetry);
}
// Vent lidt for at sikre processing
// wait for processing
await Task.Delay(5000);
// Stop servicen
_cts.Cancel();
await _service.StopAsync(CancellationToken.None);
// Assert
// Check at køen er tom
bool hasMoreMessages = await _messageChannel.Reader.WaitToReadAsync();
Assert.IsFalse(hasMoreMessages, "Køen burde være tom");
Assert.IsFalse(hasMoreMessages, "Queue should be empty after 5 seconds");
}
private class TestMessageHandler : HttpMessageHandler