Add startup option for WireMockNoNewLinesConsoleLogger

This commit is contained in:
Stef Heyenrath
2024-01-30 13:25:06 +01:00
parent 801546fae7
commit 237cd227d9
3 changed files with 36 additions and 17 deletions

View File

@@ -21,7 +21,7 @@ public sealed class WireMockContainerBuilder : ContainerBuilder<WireMockContaine
{ true, new ContainerInfo("sheyenrath/wiremock.net-windows:latest", @"c:\app\__admin\mappings") }
};
private const string DefaultLogger = "WireMockConsoleLogger";
private const string DefaultLogger = "WireMockNoNewLinesConsoleLogger";
private readonly Lazy<Task<bool>> _isWindowsAsLazy = new(async () =>
{

View File

@@ -1,5 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using WireMock.Admin.Requests;
namespace WireMock.Logging;
@@ -10,58 +10,62 @@ namespace WireMock.Logging;
/// <seealso cref="IWireMockLogger" />
public class WireMockConsoleLogger : IWireMockLogger
{
private readonly bool _removeNewLines;
/// <summary>
/// Initializes a new instance of the <see cref="WireMockConsoleLogger"/> class.
/// </summary>
public WireMockConsoleLogger()
public WireMockConsoleLogger(bool removeNewLines = false)
{
_removeNewLines = removeNewLines;
Console.OutputEncoding = System.Text.Encoding.UTF8;
}
/// <see cref="IWireMockLogger.Debug"/>
/// <inheritdoc />
public void Debug(string formatString, params object[] args)
{
Console.WriteLine(Format("Debug", formatString, args));
WriteLine(Format("Debug", formatString, args));
}
/// <see cref="IWireMockLogger.Info"/>
/// <inheritdoc />
public void Info(string formatString, params object[] args)
{
Console.WriteLine(Format("Info", formatString, args));
WriteLine(Format("Info", formatString, args));
}
/// <see cref="IWireMockLogger.Warn"/>
/// <inheritdoc />
public void Warn(string formatString, params object[] args)
{
Console.WriteLine(Format("Warn", formatString, args));
WriteLine(Format("Warn", formatString, args));
}
/// <see cref="IWireMockLogger.Error(string, object[])"/>
/// <inheritdoc />
public void Error(string formatString, params object[] args)
{
Console.WriteLine(Format("Error", formatString, args));
WriteLine(Format("Error", formatString, args));
}
/// <see cref="IWireMockLogger.Error(string, Exception)"/>
/// <inheritdoc />
public void Error(string formatString, Exception exception)
{
Console.WriteLine(Format("Error", formatString, exception.Message));
WriteLine(Format("Error", formatString, exception.Message));
if (exception is AggregateException ae)
{
ae.Handle(ex =>
{
Console.WriteLine(Format("Error", "Exception {0}", ex.Message));
WriteLine(Format("Error", "Exception {0}", ex.Message));
return true;
});
}
}
/// <see cref="IWireMockLogger.DebugRequestResponse"/>
/// <inheritdoc />
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest)
{
string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented);
Console.WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message));
WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message));
}
private static string Format(string level, string formatString, params object[] args)
@@ -70,4 +74,13 @@ public class WireMockConsoleLogger : IWireMockLogger
return $"{DateTime.UtcNow} [{level}] : {message}";
}
/// <summary>
/// Writes the specified string value, followed by the current line terminator, to the console.
/// </summary>
/// <param name="value">The value to write.</param>
private void WriteLine(string value)
{
Console.WriteLine(!_removeNewLines ? value : value.Replace(Environment.NewLine, string.Empty));
}
}

View File

@@ -83,10 +83,16 @@ public static class WireMockServerSettingsParser
private static void ParseLoggerSettings(WireMockServerSettings settings, IWireMockLogger? logger, SimpleSettingsParser parser)
{
var loggerType = parser.GetStringValue("WireMockLogger");
var replaceNewLines = parser.GetBoolValue("ReplaceNewLines");
switch (loggerType)
{
case nameof(WireMockConsoleLogger):
settings.Logger = new WireMockConsoleLogger();
settings.Logger = new WireMockConsoleLogger(replaceNewLines);
break;
case "WireMockNoNewLinesConsoleLogger":
settings.Logger = new WireMockConsoleLogger(true);
break;
case nameof(WireMockNullLogger):