Skip to content

Commit 2a8d385

Browse files
authored
Improve test failures for unexpected logs (#31406)
* Add ToString() to Testing's log WriteContext * Update StrictTestServerTests to only show unexpected log
1 parent 8508279 commit 2a8d385

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Servers/IIS/IIS/test/IIS.Tests/StrictTestServerTests.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Linq;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Testing;
67
using Microsoft.Extensions.Logging;
78
using Microsoft.Extensions.Logging.Testing;
89
using Xunit;
10+
using Xunit.Sdk;
911

1012
namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
1113
{
@@ -14,7 +16,11 @@ public class StrictTestServerTests: LoggedTest
1416
public override void Dispose()
1517
{
1618
base.Dispose();
17-
Assert.DoesNotContain(TestSink.Writes, w => w.LogLevel > LogLevel.Information);
19+
20+
if (TestSink.Writes.FirstOrDefault(w => w.LogLevel > LogLevel.Information) is WriteContext writeContext)
21+
{
22+
throw new XunitException($"Unexpected log: {writeContext}");
23+
}
1824
}
1925

2026
protected static TaskCompletionSource<bool> CreateTaskCompletionSource()

src/Testing/src/Logging/WriteContext.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ public string Message
2929
return Formatter(State, Exception);
3030
}
3131
}
32+
33+
public override string ToString()
34+
{
35+
return $"{LogLevel} {LoggerName}: {Message}";
36+
}
3237
}
33-
}
38+
}

0 commit comments

Comments
 (0)