Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test failures for unexpected logs #31406

Merged
2 commits merged into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
using Xunit.Sdk;

namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
{
Expand All @@ -14,7 +16,11 @@ public class StrictTestServerTests: LoggedTest
public override void Dispose()
{
base.Dispose();
Assert.DoesNotContain(TestSink.Writes, w => w.LogLevel > LogLevel.Information);

if (TestSink.Writes.FirstOrDefault(w => w.LogLevel > LogLevel.Information) is WriteContext writeContext)
{
throw new XunitException($"Unexpected log: {writeContext}");
}
}

protected static TaskCompletionSource<bool> CreateTaskCompletionSource()
Expand Down
7 changes: 6 additions & 1 deletion src/Testing/src/Logging/WriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ public string Message
return Formatter(State, Exception);
}
}

public override string ToString()
{
return $"{LogLevel} {LoggerName}: {Message}";
}
}
}
}