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 _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 _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 (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); await _service.StopAsync(CancellationToken.None); bool hasMoreMessages = await _messageChannel.Reader.WaitToReadAsync(); Assert.IsFalse(hasMoreMessages, "Queue should be empty after 5 seconds"); } } }