// Copyright © WireMock.Net using System; using Newtonsoft.Json; using Stef.Validation; using WireMock.Admin.Requests; using WireMock.Logging; using Xunit.Abstractions; namespace WireMock.Net.Xunit; /// /// When using xUnit, this class enables to log the output from WireMock.Net to the . /// public sealed class TestOutputHelperWireMockLogger : IWireMockLogger { private readonly ITestOutputHelper _testOutputHelper; /// /// Create a new instance on the . /// /// Represents a class which can be used to provide test output. public TestOutputHelperWireMockLogger(ITestOutputHelper testOutputHelper) { _testOutputHelper = Guard.NotNull(testOutputHelper); } /// public void Debug(string formatString, params object[] args) { _testOutputHelper.WriteLine(Format("Debug", formatString, args)); } /// public void Info(string formatString, params object[] args) { _testOutputHelper.WriteLine(Format("Info", formatString, args)); } /// public void Warn(string formatString, params object[] args) { _testOutputHelper.WriteLine(Format("Warning", formatString, args)); } /// public void Error(string formatString, params object[] args) { _testOutputHelper.WriteLine(Format("Error", formatString, args)); } /// public void Error(string formatString, Exception exception) { _testOutputHelper.WriteLine(Format("Error", formatString, exception.Message)); if (exception is AggregateException ae) { ae.Handle(ex => { _testOutputHelper.WriteLine(Format("Error", "Exception {0}", ex.Message)); return true; }); } } /// public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) { var message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented); _testOutputHelper.WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message)); } private static string Format(string level, string formatString, params object[] args) { Guard.NotNull(formatString); var message = args.Length > 0 ? string.Format(formatString, args) : formatString; return $"{DateTime.UtcNow} [{level}] : {message}"; } }