using Autofac; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts; using PlanTempus.Core.Logging; using PlanTempus.Core.Telemetry; namespace PlanTempus.X.TDD.Logging; [TestClass] public class SeqTelemetryChannelTest : TestFixture { private CancellationTokenSource _cts; private IMessageChannel _messageChannel; private SeqBackgroundService _service; private TelemetryClient _telemetryClient; [TestInitialize] public void SetupThis() { //it is important to use the same MessageChannel as the BackgroundService uses //we know that IMessageChannel _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>(); _service = Container.Resolve(); _telemetryClient = Container.Resolve(); _cts = new CancellationTokenSource(); } [TestMethod] public async Task Messages_ShouldBeProcessedFromQueue() { await _service.StartAsync(_cts.Token); for (var 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); await _service.StopAsync(CancellationToken.None); var hasMoreMessages = await _messageChannel.Reader.WaitToReadAsync(); Assert.IsFalse(hasMoreMessages, "Queue should be empty after 5 seconds"); } }