Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add startup option for WireMockNoNewLinesConsoleLogger #1063

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ For more details see also [Docker](https://github.com/WireMock-Net/WireMock.Net-

#### HTTPS / SSL
More details on using HTTPS (SSL) can be found here [Wiki : HTTPS](https://github.com/WireMock-Net/WireMock.Net/wiki/Using-HTTPS-(SSL))

4 changes: 2 additions & 2 deletions src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () =>
{
Expand Down Expand Up @@ -157,7 +157,7 @@ protected override WireMockContainerBuilder Init()
return builder
.WithPortBinding(WireMockContainer.ContainerPort, true)
.WithCommand($"--WireMockLogger {DefaultLogger}")
.WithWaitStrategy(waitForContainerOS.UntilMessageIsLogged("By Stef Heyenrath"));
.WithWaitStrategy(waitForContainerOS.UntilMessageIsLogged("WireMock.Net server running"));
}

/// <inheritdoc />
Expand Down
46 changes: 31 additions & 15 deletions src/WireMock.Net/Logging/WireMockConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using WireMock.Admin.Requests;

namespace WireMock.Logging;
Expand All @@ -10,58 +10,65 @@ namespace WireMock.Logging;
/// <seealso cref="IWireMockLogger" />
public class WireMockConsoleLogger : IWireMockLogger
{
private const string NewlineWindows = "\r\n";
private const string NewlineUnix = "\n";

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)
Expand All @@ -70,4 +77,13 @@ private static string Format(string level, string formatString, params object[]

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(NewlineWindows, string.Empty).Replace(NewlineUnix, string.Empty));
}
}
8 changes: 7 additions & 1 deletion src/WireMock.Net/Settings/WireMockServerSettingsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ public static bool TryParseArguments(string[] args, IDictionary? environment, [N
private static void ParseLoggerSettings(WireMockServerSettings settings, IWireMockLogger? logger, SimpleSettingsParser parser)
{
var loggerType = parser.GetStringValue("WireMockLogger");
var removeNewLines = parser.GetBoolValue("RemoveNewLines");

switch (loggerType)
{
case nameof(WireMockConsoleLogger):
settings.Logger = new WireMockConsoleLogger();
settings.Logger = new WireMockConsoleLogger(removeNewLines);
break;

case "WireMockNoNewLinesConsoleLogger":
settings.Logger = new WireMockConsoleLogger(true);
break;

case nameof(WireMockNullLogger):
Expand Down
Loading