diff --git a/src/WireMock.Net.xUnit/TestOutputHelperWireMockLogger.cs b/src/WireMock.Net.xUnit/TestOutputHelperWireMockLogger.cs index bda59781..bfedd2ed 100644 --- a/src/WireMock.Net.xUnit/TestOutputHelperWireMockLogger.cs +++ b/src/WireMock.Net.xUnit/TestOutputHelperWireMockLogger.cs @@ -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; /// public sealed class TestOutputHelperWireMockLogger : IWireMockLogger { - private readonly ITestOutputHelper _testOutputHelper; + private readonly Func _testOutputHelperFactory; /// /// Create a new instance on the . /// /// Represents a class which can be used to provide test output. - public TestOutputHelperWireMockLogger(ITestOutputHelper testOutputHelper) + public TestOutputHelperWireMockLogger(ITestOutputHelper testOutputHelper) : + this(() => testOutputHelper) { - _testOutputHelper = Guard.NotNull(testOutputHelper); + Guard.NotNull(testOutputHelper); + } + + /// + /// Create a new instance on the . + /// + /// Represents a factory to provide current test output. + public TestOutputHelperWireMockLogger(Func testOutputHelperFactory) + { + _testOutputHelperFactory = Guard.NotNull(testOutputHelperFactory); } /// public void Debug(string formatString, params object[] args) { - _testOutputHelper.WriteLine(Format("Debug", formatString, args)); + _testOutputHelperFactory()?.WriteLine(Format("Debug", formatString, args)); } /// public void Info(string formatString, params object[] args) { - _testOutputHelper.WriteLine(Format("Info", formatString, args)); + _testOutputHelperFactory()?.WriteLine(Format("Info", formatString, args)); } /// public void Warn(string formatString, params object[] args) { - _testOutputHelper.WriteLine(Format("Warning", formatString, args)); + _testOutputHelperFactory()?.WriteLine(Format("Warning", formatString, args)); } /// public void Error(string formatString, params object[] args) { - _testOutputHelper.WriteLine(Format("Error", formatString, args)); + _testOutputHelperFactory()?.WriteLine(Format("Error", formatString, args)); } /// 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)