This is near the end of this Seq Logging Implementation

This commit is contained in:
Janus C. H. Knudsen 2025-02-22 20:14:56 +01:00
parent 78d49a9829
commit 099f6467d2
9 changed files with 212 additions and 123 deletions

View file

@ -0,0 +1,70 @@
using Autofac;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using PlanTempus.Core.Logging;
using PlanTempus.Core.Telemetry;
namespace PlanTempus.Tests.Logging
{
[TestClass]
public class SeqTelemetryChannelTest : TestFixture
{
private IMessageChannel<ITelemetry> _messageChannel;
TelemetryClient _telemetryClient;
private SeqBackgroundService _service;
private CancellationTokenSource _cts;
[TestInitialize]
public void SetupThis()
{
//it is important to use the same MessageChannel as the BackgroundService uses
//we know that IMessageChannel<ITelemetry> _messageChannel; is registered via Autofac and manually injected into SeqBackgroundService
//so we can get it by calling the Autofac Container in this test.
_messageChannel = Container.Resolve<IMessageChannel<ITelemetry>>();
_service = Container.Resolve<SeqBackgroundService>();
_telemetryClient = Container.Resolve<TelemetryClient>();
_cts = new CancellationTokenSource();
}
[TestMethod]
public async Task Messages_ShouldBeProcessedFromQueue()
{
await _service.StartAsync(_cts.Token);
for (int i = 0; i < 5; i++)
{
var eventTelemetry = new EventTelemetry
{
Name = "Test Event 3",
Timestamp = DateTimeOffset.UtcNow
};
eventTelemetry.Properties.Add("TestId", Guid.NewGuid().ToString());
eventTelemetry.Metrics.Add("TestMetric", 42.0);
//we don't write to the _messageChannel.Writer.WriteAsync(eventTelemetry);, but the TelemetryClient which is configured to use SeqTelemetryChannel
_telemetryClient.TrackEvent(eventTelemetry);
}
// wait for processing
await Task.Delay(5000);
//_cts.Cancel();
await _service.StopAsync(CancellationToken.None);
bool hasMoreMessages = await _messageChannel.Reader.WaitToReadAsync();
Assert.IsFalse(hasMoreMessages, "Queue should be empty after 5 seconds");
}
[TestCleanup]
public void Cleanup()
{
_cts?.Dispose();
}
}
}