Skip to content

Commit

Permalink
Add more app logging to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebazzz committed Nov 13, 2024
1 parent cbec81f commit ecea3f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 11 additions & 8 deletions tests/Return.Web.Tests.Integration/Common/ReturnAppFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,17 @@ private void ConfigureWebHost(IHostBuilder builder) {

// Configure testing to use Kestel and test services
builder
.ConfigureLogging(lb => {
lb.SetMinimumLevel(LogLevel.Trace);
lb.AddProvider(new TestContextLoggerProvider());

string logFileName = (TestContext.CurrentContext?.Test.ClassName ?? "test-log") + ".log";
lb.AddFile(Path.Join(Paths.TestArtifactDir, logFileName));
})
.UseSerilog()
.UseSerilog(
(ctx, loggerConfig) =>
{
loggerConfig.WriteTo.File(
path:Path.Join(Paths.TestArtifactDir, $"test-log-{DateTime.Now.Ticks}.log"),
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}",
shared: true
);
loggerConfig.WriteTo.Sink(new TestContextLoggerProvider());
}
)
.ConfigureWebHostDefaults(wb =>
{
wb.UseStaticWebAssets().UseKestrel(k => k.Listen(endPoint)).UseStartup<Startup>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ namespace Return.Web.Tests.Integration.Common;
using System;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using Serilog.Core;
using Serilog.Events;

public sealed class TestContextLoggerProvider : ILoggerProvider {
public sealed class TestContextLoggerProvider : ILoggerProvider, ILogEventSink
{
public void Dispose() {
TestContext.Progress.WriteLine($"{typeof(TestContextLogger)}: Dispose");
}

public ILogger CreateLogger(string categoryName) => new TestContextLogger(categoryName);

public void Emit(LogEvent logEvent) => logEvent.RenderMessage(TestContext.Progress);
}

internal sealed class TestContextLogger : ILogger {
Expand Down

0 comments on commit ecea3f2

Please sign in to comment.