Skip to content
Merged
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
31 changes: 20 additions & 11 deletions src/WireMock.Net.xUnit/TestOutputHelperWireMockLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net

using System;
using Newtonsoft.Json;
using Stef.Validation;
using WireMock.Admin.Requests;
Expand All @@ -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;
});
}
Expand All @@ -72,7 +81,7 @@ public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminReques
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)
Expand Down
Loading