This is near the end of this Seq Logging Implementation
This commit is contained in:
parent
78d49a9829
commit
099f6467d2
9 changed files with 212 additions and 123 deletions
|
|
@ -32,9 +32,7 @@ namespace PlanTempus.Tests.Logging
|
|||
[TestMethod]
|
||||
public async Task Messages_ShouldBeProcessedFromQueue()
|
||||
{
|
||||
var processedMessages = new List<HttpRequestMessage>();
|
||||
|
||||
var serviceTask = _service.StartAsync(_cts.Token);
|
||||
await _service.StartAsync(_cts.Token);
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -112,6 +112,10 @@ namespace PlanTempus.Tests.Logging
|
|||
await _logger.LogAsync(dependencyTelemetry);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is for scope test in SeqLogger. It is not testing anything related to the TelemetryChannel which logs to Seq.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[TestMethod]
|
||||
public async Task LogRequestTelemetryInOperationHolderWithParentChild_SendsCorrectData()
|
||||
{
|
||||
|
|
|
|||
70
Tests/Logging/SeqTelemetryChannelTest.cs
Normal file
70
Tests/Logging/SeqTelemetryChannelTest.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,84 +8,89 @@ using PlanTempus.Core.Configurations.JsonConfigProvider;
|
|||
using PlanTempus.Core.ModuleRegistry;
|
||||
namespace PlanTempus.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Act as base class for tests. Avoids duplication of test setup code
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public abstract partial class TestFixture
|
||||
{
|
||||
private readonly string _configurationFilePath;
|
||||
/// <summary>
|
||||
/// Act as base class for tests. Avoids duplication of test setup code
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public abstract partial class TestFixture
|
||||
{
|
||||
private readonly string _configurationFilePath;
|
||||
|
||||
protected IContainer Container { get; private set; }
|
||||
protected ContainerBuilder ContainerBuilder { get; private set; }
|
||||
protected IContainer Container { get; private set; }
|
||||
protected ContainerBuilder ContainerBuilder { get; private set; }
|
||||
|
||||
public virtual IConfigurationRoot Configuration()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile($"{_configurationFilePath}appconfiguration.dev.json")
|
||||
.Build();
|
||||
public virtual IConfigurationRoot Configuration()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile($"{_configurationFilePath}appconfiguration.dev.json")
|
||||
.Build();
|
||||
|
||||
return configuration;
|
||||
}
|
||||
return configuration;
|
||||
}
|
||||
|
||||
protected TestFixture() : this(null) { }
|
||||
public TestFixture(string configurationFilePath)
|
||||
{
|
||||
if (configurationFilePath is not null)
|
||||
_configurationFilePath = configurationFilePath?.TrimEnd('/') + "/";
|
||||
protected TestFixture() : this(null) { }
|
||||
public TestFixture(string configurationFilePath)
|
||||
{
|
||||
if (configurationFilePath is not null)
|
||||
_configurationFilePath = configurationFilePath?.TrimEnd('/') + "/";
|
||||
|
||||
CreateContainerBuilder();
|
||||
Container = ContainerBuilder.Build();
|
||||
}
|
||||
protected virtual void CreateContainerBuilder()
|
||||
{
|
||||
IConfigurationRoot configuration = Configuration();
|
||||
CreateContainerBuilder();
|
||||
Container = ContainerBuilder.Build();
|
||||
}
|
||||
protected virtual void CreateContainerBuilder()
|
||||
{
|
||||
IConfigurationRoot configuration = Configuration();
|
||||
|
||||
//var logger = new LoggerConfiguration()
|
||||
// .MinimumLevel.Verbose()
|
||||
// .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
|
||||
// .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error)
|
||||
// .WriteTo.Seq("http://localhost:5341", apiKey: "Gt8hS9ClGNfOCAdswDlW")
|
||||
// .WriteTo.ApplicationInsights(configuration.Get<string>("ApplicationInsights:ConnectionString"),
|
||||
// TelemetryConverter.Traces)
|
||||
// .Enrich.FromLogContext()
|
||||
// .Enrich.WithMachineName()
|
||||
// .CreateLogger();
|
||||
//var logger = new LoggerConfiguration()
|
||||
// .MinimumLevel.Verbose()
|
||||
// .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
|
||||
// .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error)
|
||||
// .WriteTo.Seq("http://localhost:5341", apiKey: "Gt8hS9ClGNfOCAdswDlW")
|
||||
// .WriteTo.ApplicationInsights(configuration.Get<string>("ApplicationInsights:ConnectionString"),
|
||||
// TelemetryConverter.Traces)
|
||||
// .Enrich.FromLogContext()
|
||||
// .Enrich.WithMachineName()
|
||||
// .CreateLogger();
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
builder.RegisterGeneric(typeof(Logger<>))
|
||||
.As(typeof(ILogger<>))
|
||||
.SingleInstance();
|
||||
builder.RegisterGeneric(typeof(Logger<>))
|
||||
.As(typeof(ILogger<>))
|
||||
.SingleInstance();
|
||||
|
||||
|
||||
builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
});
|
||||
builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
});
|
||||
|
||||
builder.RegisterModule(new TelemetryModule
|
||||
{
|
||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||
});
|
||||
builder.RegisterModule(new TelemetryModule
|
||||
{
|
||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||
});
|
||||
builder.RegisterModule(new SeqLoggingModule
|
||||
{
|
||||
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<Core.Logging.SeqConfiguration>()
|
||||
|
||||
});
|
||||
|
||||
|
||||
ContainerBuilder = builder;
|
||||
}
|
||||
ContainerBuilder = builder;
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
Trace.Flush();
|
||||
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
telemetryClient.Flush();
|
||||
[TestCleanup]
|
||||
public void CleanUp()
|
||||
{
|
||||
Trace.Flush();
|
||||
var telemetryClient = Container.Resolve<TelemetryClient>();
|
||||
telemetryClient.Flush();
|
||||
|
||||
if (Container != null)
|
||||
{
|
||||
Container.Dispose();
|
||||
Container = null;
|
||||
}
|
||||
}
|
||||
if (Container != null)
|
||||
{
|
||||
Container.Dispose();
|
||||
Container = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue