Skip to content

Commit ecea3f2

Browse files
committed
Add more app logging to the tests
1 parent cbec81f commit ecea3f2

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

tests/Return.Web.Tests.Integration/Common/ReturnAppFactory.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,17 @@ private void ConfigureWebHost(IHostBuilder builder) {
161161

162162
// Configure testing to use Kestel and test services
163163
builder
164-
.ConfigureLogging(lb => {
165-
lb.SetMinimumLevel(LogLevel.Trace);
166-
lb.AddProvider(new TestContextLoggerProvider());
167-
168-
string logFileName = (TestContext.CurrentContext?.Test.ClassName ?? "test-log") + ".log";
169-
lb.AddFile(Path.Join(Paths.TestArtifactDir, logFileName));
170-
})
171-
.UseSerilog()
164+
.UseSerilog(
165+
(ctx, loggerConfig) =>
166+
{
167+
loggerConfig.WriteTo.File(
168+
path:Path.Join(Paths.TestArtifactDir, $"test-log-{DateTime.Now.Ticks}.log"),
169+
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}",
170+
shared: true
171+
);
172+
loggerConfig.WriteTo.Sink(new TestContextLoggerProvider());
173+
}
174+
)
172175
.ConfigureWebHostDefaults(wb =>
173176
{
174177
wb.UseStaticWebAssets().UseKestrel(k => k.Listen(endPoint)).UseStartup<Startup>();

tests/Return.Web.Tests.Integration/Common/TestContextLogger.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ namespace Return.Web.Tests.Integration.Common;
1010
using System;
1111
using Microsoft.Extensions.Logging;
1212
using NUnit.Framework;
13+
using Serilog.Core;
14+
using Serilog.Events;
1315

14-
public sealed class TestContextLoggerProvider : ILoggerProvider {
16+
public sealed class TestContextLoggerProvider : ILoggerProvider, ILogEventSink
17+
{
1518
public void Dispose() {
1619
TestContext.Progress.WriteLine($"{typeof(TestContextLogger)}: Dispose");
1720
}
1821

1922
public ILogger CreateLogger(string categoryName) => new TestContextLogger(categoryName);
23+
24+
public void Emit(LogEvent logEvent) => logEvent.RenderMessage(TestContext.Progress);
2025
}
2126

2227
internal sealed class TestContextLogger : ILogger {

0 commit comments

Comments
 (0)