feat(xunit logging): TestOutputHelperWireMockLogger (#1488)

* feat(xunit logging): TestOutputHelperWireMockLogger

allow to provide current test output helper

* fix(ctor): TestOutputHelperWireMockLogger

made factory return nullable

---------

Co-authored-by: Минин Степан Александрович <minin_s@goldapple.ru>
This commit is contained in:
Степан
2026-07-17 19:25:25 +03:00
committed by GitHub
parent 345efe8cfc
commit d270ad7f4f
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System;
using Newtonsoft.Json;
using Stef.Validation;
using WireMock.Admin.Requests;
@@ -14,51 +13,61 @@ namespace WireMock.Net.Xunit;
/// </summary>
public sealed class TestOutputHelperWireMockLogger : IWireMockLogger
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly Func<ITestOutputHelper?> _testOutputHelperFactory;
/// <summary>
/// Create a new instance on the <see cref="TestOutputHelperWireMockLogger"/>.
/// </summary>
/// <param name="testOutputHelper">Represents a class which can be used to provide test output.</param>
public TestOutputHelperWireMockLogger(ITestOutputHelper testOutputHelper)
public TestOutputHelperWireMockLogger(ITestOutputHelper testOutputHelper) :
this(() => testOutputHelper)
{
_testOutputHelper = Guard.NotNull(testOutputHelper);
Guard.NotNull(testOutputHelper);
}
/// <summary>
/// Create a new instance on the <see cref="TestOutputHelperWireMockLogger"/>.
/// </summary>
/// <param name="testOutputHelperFactory">Represents a factory to provide current test output.</param>
public TestOutputHelperWireMockLogger(Func<ITestOutputHelper?> testOutputHelperFactory)
{
_testOutputHelperFactory = Guard.NotNull(testOutputHelperFactory);
}
/// <inheritdoc />
public void Debug(string formatString, params object[] args)
{
_testOutputHelper.WriteLine(Format("Debug", formatString, args));
_testOutputHelperFactory()?.WriteLine(Format("Debug", formatString, args));
}
/// <inheritdoc />
public void Info(string formatString, params object[] args)
{
_testOutputHelper.WriteLine(Format("Info", formatString, args));
_testOutputHelperFactory()?.WriteLine(Format("Info", formatString, args));
}
/// <inheritdoc />
public void Warn(string formatString, params object[] args)
{
_testOutputHelper.WriteLine(Format("Warning", formatString, args));
_testOutputHelperFactory()?.WriteLine(Format("Warning", formatString, args));
}
/// <inheritdoc />
public void Error(string formatString, params object[] args)
{
_testOutputHelper.WriteLine(Format("Error", formatString, args));
_testOutputHelperFactory()?.WriteLine(Format("Error", formatString, args));
}
/// <inheritdoc />
public void Error(string message, Exception exception)
{
_testOutputHelper.WriteLine(Format("Error", $"{message} {{0}}", exception));
_testOutputHelperFactory()?.WriteLine(Format("Error", $"{message} {{0}}", exception));
if (exception is AggregateException ae)
{
ae.Handle(ex =>
{
_testOutputHelper.WriteLine(Format("Error", "Exception {0}", ex));
_testOutputHelperFactory()?.WriteLine(Format("Error", "Exception {0}", ex));
return true;
});
}
@@ -72,7 +81,7 @@ public sealed class TestOutputHelperWireMockLogger : IWireMockLogger
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore
});
_testOutputHelper.WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message));
_testOutputHelperFactory()?.WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message));
}
private static string Format(string level, string formatString, params object[] args)