mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-07-18 10:41:26 +02:00
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:
@@ -1,6 +1,5 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Admin.Requests;
|
using WireMock.Admin.Requests;
|
||||||
@@ -14,51 +13,61 @@ namespace WireMock.Net.Xunit;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class TestOutputHelperWireMockLogger : IWireMockLogger
|
public sealed class TestOutputHelperWireMockLogger : IWireMockLogger
|
||||||
{
|
{
|
||||||
private readonly ITestOutputHelper _testOutputHelper;
|
private readonly Func<ITestOutputHelper?> _testOutputHelperFactory;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new instance on the <see cref="TestOutputHelperWireMockLogger"/>.
|
/// Create a new instance on the <see cref="TestOutputHelperWireMockLogger"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="testOutputHelper">Represents a class which can be used to provide test output.</param>
|
/// <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 />
|
/// <inheritdoc />
|
||||||
public void Debug(string formatString, params object[] args)
|
public void Debug(string formatString, params object[] args)
|
||||||
{
|
{
|
||||||
_testOutputHelper.WriteLine(Format("Debug", formatString, args));
|
_testOutputHelperFactory()?.WriteLine(Format("Debug", formatString, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Info(string formatString, params object[] args)
|
public void Info(string formatString, params object[] args)
|
||||||
{
|
{
|
||||||
_testOutputHelper.WriteLine(Format("Info", formatString, args));
|
_testOutputHelperFactory()?.WriteLine(Format("Info", formatString, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Warn(string formatString, params object[] args)
|
public void Warn(string formatString, params object[] args)
|
||||||
{
|
{
|
||||||
_testOutputHelper.WriteLine(Format("Warning", formatString, args));
|
_testOutputHelperFactory()?.WriteLine(Format("Warning", formatString, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Error(string formatString, params object[] args)
|
public void Error(string formatString, params object[] args)
|
||||||
{
|
{
|
||||||
_testOutputHelper.WriteLine(Format("Error", formatString, args));
|
_testOutputHelperFactory()?.WriteLine(Format("Error", formatString, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void Error(string message, Exception exception)
|
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)
|
if (exception is AggregateException ae)
|
||||||
{
|
{
|
||||||
ae.Handle(ex =>
|
ae.Handle(ex =>
|
||||||
{
|
{
|
||||||
_testOutputHelper.WriteLine(Format("Error", "Exception {0}", ex));
|
_testOutputHelperFactory()?.WriteLine(Format("Error", "Exception {0}", ex));
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -72,7 +81,7 @@ public sealed class TestOutputHelperWireMockLogger : IWireMockLogger
|
|||||||
Formatting = Formatting.Indented,
|
Formatting = Formatting.Indented,
|
||||||
NullValueHandling = NullValueHandling.Ignore
|
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)
|
private static string Format(string level, string formatString, params object[] args)
|
||||||
|
|||||||
Reference in New Issue
Block a user