From 7fd1d30d0ecad58ac9699114db2be7d303a22848 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 6 Nov 2022 13:25:26 +0100 Subject: [PATCH] Add WireMockNullLogger as valid commandline logger option (#845) * Add WireMockNullLogger as valid commandline logger option * . --- .../Logging/WireMockConsoleLogger.cs | 111 +++++++++--------- .../Logging/WireMockNullLogger.cs | 73 ++++++------ .../Settings/WireMockServerSettingsParser.cs | 22 +++- src/WireMock.Net/WireMock.Net.csproj | 5 - src/dotnet-WireMock.Net/Program.cs | 81 +++++++------ .../Properties/launchSettings.json | 4 +- src/dotnet-WireMock.Net/WireMockLogger.cs | 83 +++++++------ 7 files changed, 190 insertions(+), 189 deletions(-) diff --git a/src/WireMock.Net/Logging/WireMockConsoleLogger.cs b/src/WireMock.Net/Logging/WireMockConsoleLogger.cs index 765071b2..f798c435 100644 --- a/src/WireMock.Net/Logging/WireMockConsoleLogger.cs +++ b/src/WireMock.Net/Logging/WireMockConsoleLogger.cs @@ -2,73 +2,72 @@ using Newtonsoft.Json; using System; using WireMock.Admin.Requests; -namespace WireMock.Logging +namespace WireMock.Logging; + +/// +/// WireMockConsoleLogger which logs to Console +/// +/// +public class WireMockConsoleLogger : IWireMockLogger { /// - /// WireMockConsoleLogger which logs to Console + /// Initializes a new instance of the class. /// - /// - public class WireMockConsoleLogger : IWireMockLogger + public WireMockConsoleLogger() { - /// - /// Initializes a new instance of the class. - /// - public WireMockConsoleLogger() - { - Console.OutputEncoding = System.Text.Encoding.UTF8; - } + Console.OutputEncoding = System.Text.Encoding.UTF8; + } - /// - public void Debug(string formatString, params object[] args) - { - Console.WriteLine(Format("Debug", formatString, args)); - } + /// + public void Debug(string formatString, params object[] args) + { + Console.WriteLine(Format("Debug", formatString, args)); + } - /// - public void Info(string formatString, params object[] args) - { - Console.WriteLine(Format("Info", formatString, args)); - } + /// + public void Info(string formatString, params object[] args) + { + Console.WriteLine(Format("Info", formatString, args)); + } - /// - public void Warn(string formatString, params object[] args) - { - Console.WriteLine(Format("Warn", formatString, args)); - } + /// + public void Warn(string formatString, params object[] args) + { + Console.WriteLine(Format("Warn", formatString, args)); + } - /// - public void Error(string formatString, params object[] args) - { - Console.WriteLine(Format("Error", formatString, args)); - } + /// + public void Error(string formatString, params object[] args) + { + Console.WriteLine(Format("Error", formatString, args)); + } - /// - public void Error(string formatString, Exception exception) - { - Console.WriteLine(Format("Error", formatString, exception.Message)); + /// + public void Error(string formatString, Exception exception) + { + Console.WriteLine(Format("Error", formatString, exception.Message)); - if (exception is AggregateException ae) + if (exception is AggregateException ae) + { + ae.Handle(ex => { - ae.Handle(ex => - { - Console.WriteLine(Format("Error", "Exception {0}", ex.Message)); - return true; - }); - } - } - - /// - public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) - { - string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented); - Console.WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message)); - } - - private static string Format(string level, string formatString, params object[] args) - { - var message = args.Length > 0 ? string.Format(formatString, args) : formatString; - - return $"{DateTime.UtcNow} [{level}] : {message}"; + Console.WriteLine(Format("Error", "Exception {0}", ex.Message)); + return true; + }); } } + + /// + public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) + { + string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented); + Console.WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message)); + } + + private static string Format(string level, string formatString, params object[] args) + { + var message = args.Length > 0 ? string.Format(formatString, args) : formatString; + + return $"{DateTime.UtcNow} [{level}] : {message}"; + } } \ No newline at end of file diff --git a/src/WireMock.Net/Logging/WireMockNullLogger.cs b/src/WireMock.Net/Logging/WireMockNullLogger.cs index 8e55a69d..21df995c 100644 --- a/src/WireMock.Net/Logging/WireMockNullLogger.cs +++ b/src/WireMock.Net/Logging/WireMockNullLogger.cs @@ -1,48 +1,47 @@ -using System; +using System; using WireMock.Admin.Requests; -namespace WireMock.Logging +namespace WireMock.Logging; + +/// +/// WireMockNullLogger which does not log. +/// +/// +public class WireMockNullLogger : IWireMockLogger { - /// - /// WireMockNullLogger which does not log. - /// - /// - public class WireMockNullLogger : IWireMockLogger + /// + public void Debug(string formatString, params object[] args) { - /// - public void Debug(string formatString, params object[] args) - { - // Log nothing - } + // Log nothing + } - /// - public void Info(string formatString, params object[] args) - { - // Log nothing - } + /// + public void Info(string formatString, params object[] args) + { + // Log nothing + } - /// - public void Warn(string formatString, params object[] args) - { - // Log nothing - } + /// + public void Warn(string formatString, params object[] args) + { + // Log nothing + } - /// - public void Error(string formatString, params object[] args) - { - // Log nothing - } + /// + public void Error(string formatString, params object[] args) + { + // Log nothing + } - /// - public void Error(string formatString, Exception exception) - { - // Log nothing - } + /// + public void Error(string formatString, Exception exception) + { + // Log nothing + } - /// - public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) - { - // Log nothing - } + /// + public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) + { + // Log nothing } } \ No newline at end of file diff --git a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs index 1e1b6625..f9b77879 100644 --- a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs +++ b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs @@ -62,13 +62,23 @@ public static class WireMockServerSettingsParser settings.CorsPolicyOptions = parser.GetEnumValue(nameof(WireMockServerSettings.CorsPolicyOptions), CorsPolicyOptions.None); #endif - if (logger != null) + var loggerType = parser.GetStringValue("WireMockLogger"); + switch (loggerType) { - settings.Logger = logger; - } - else if (parser.GetStringValue("WireMockLogger") == "WireMockConsoleLogger") - { - settings.Logger = new WireMockConsoleLogger(); + case nameof(WireMockConsoleLogger): + settings.Logger = new WireMockConsoleLogger(); + break; + + case nameof(WireMockNullLogger): + settings.Logger = new WireMockNullLogger(); + break; + + default: + if (logger != null) + { + settings.Logger = logger; + } + break; } if (parser.Contains(nameof(WireMockServerSettings.Port))) diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj index cb0a1e24..4aa6fce6 100644 --- a/src/WireMock.Net/WireMock.Net.csproj +++ b/src/WireMock.Net/WireMock.Net.csproj @@ -21,10 +21,6 @@ true - - @@ -145,7 +141,6 @@ - diff --git a/src/dotnet-WireMock.Net/Program.cs b/src/dotnet-WireMock.Net/Program.cs index 49cb9858..d5200bad 100644 --- a/src/dotnet-WireMock.Net/Program.cs +++ b/src/dotnet-WireMock.Net/Program.cs @@ -5,56 +5,55 @@ using WireMock.Logging; using WireMock.Net.StandAlone; using WireMock.Server; -namespace WireMock.Net +namespace WireMock.Net; + +public class Program { - public class Program + private static readonly int SleepTime = 30000; + private static readonly ILogger XLogger = LoggerFactory.Create(o => { - private static readonly int SleepTime = 30000; - private static readonly ILogger xLogger = LoggerFactory.Create(o => + o.SetMinimumLevel(LogLevel.Debug); + o.AddSimpleConsole(options => { - o.SetMinimumLevel(LogLevel.Debug); - o.AddSimpleConsole(options => - { - options.IncludeScopes = true; - options.SingleLine = false; - options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss "; - }); - }).CreateLogger("WireMock.Net"); - private static readonly IWireMockLogger Logger = new WireMockLogger(xLogger); + options.IncludeScopes = true; + options.SingleLine = false; + options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss "; + }); + }).CreateLogger("WireMock.Net"); + private static readonly IWireMockLogger Logger = new WireMockLogger(XLogger); - private static WireMockServer Server; + private static WireMockServer _server = null!; - static async Task Main(string[] args) + static async Task Main(string[] args) + { + if (!StandAloneApp.TryStart(args, out _server!, Logger)) { - if (!StandAloneApp.TryStart(args, out Server, Logger)) - { - return; - } - - Logger.Info("Press Ctrl+C to shut down"); - - Console.CancelKeyPress += (s, e) => - { - Stop("CancelKeyPress"); - }; - - System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx => - { - Stop("AssemblyLoadContext.Default.Unloading"); - }; - - while (true) - { - Logger.Info("Server running : {IsStarted}", Server.IsStarted); - await Task.Delay(SleepTime).ConfigureAwait(false); - } + return; } - private static void Stop(string why) + Logger.Info("Press Ctrl+C to shut down"); + + Console.CancelKeyPress += (s, e) => { - Logger.Info("Server stopping because '{why}'", why); - Server.Stop(); - Logger.Info("Server stopped"); + Stop("CancelKeyPress"); + }; + + System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx => + { + Stop("AssemblyLoadContext.Default.Unloading"); + }; + + while (true) + { + Logger.Info("Server running : {0}", _server.IsStarted); + await Task.Delay(SleepTime).ConfigureAwait(false); } } + + private static void Stop(string why) + { + Logger.Info("Server stopping because '{0}'", why); + _server.Stop(); + Logger.Info("Server stopped"); + } } \ No newline at end of file diff --git a/src/dotnet-WireMock.Net/Properties/launchSettings.json b/src/dotnet-WireMock.Net/Properties/launchSettings.json index e2222cce..6bd71fa2 100644 --- a/src/dotnet-WireMock.Net/Properties/launchSettings.json +++ b/src/dotnet-WireMock.Net/Properties/launchSettings.json @@ -1,8 +1,8 @@ { "profiles": { "dotnet-WireMock.Net": { - "commandName": "Project", - "commandLineArgs": "" + "commandName": "Project", + "commandLineArgs": "--WireMockLogger s" } } } \ No newline at end of file diff --git a/src/dotnet-WireMock.Net/WireMockLogger.cs b/src/dotnet-WireMock.Net/WireMockLogger.cs index 3cc2e459..535feb67 100644 --- a/src/dotnet-WireMock.Net/WireMockLogger.cs +++ b/src/dotnet-WireMock.Net/WireMockLogger.cs @@ -4,58 +4,57 @@ using Microsoft.Extensions.Logging; using WireMock.Admin.Requests; using WireMock.Logging; -namespace WireMock.Net +namespace WireMock.Net; + +public class WireMockLogger : IWireMockLogger { - public class WireMockLogger : IWireMockLogger + private readonly JsonSerializerOptions _options = new() { - private readonly JsonSerializerOptions options = new JsonSerializerOptions - { - WriteIndented = true - }; + WriteIndented = true + }; - private readonly ILogger _logger; + private readonly ILogger _logger; - public WireMockLogger(ILogger logger) - { - _logger = logger; - } + public WireMockLogger(ILogger logger) + { + _logger = logger; + } - /// - public void Debug(string formatString, params object[] args) - { - _logger.LogDebug(formatString, args); - } + /// + public void Debug(string formatString, params object[] args) + { + _logger.LogDebug(formatString, args); + } - /// - public void Info(string formatString, params object[] args) - { - _logger.LogInformation(formatString, args); - } + /// + public void Info(string formatString, params object[] args) + { + _logger.LogInformation(formatString, args); + } - /// - public void Warn(string formatString, params object[] args) - { - _logger.LogWarning(formatString, args); - } + /// + public void Warn(string formatString, params object[] args) + { + _logger.LogWarning(formatString, args); + } - /// - public void Error(string formatString, params object[] args) - { - _logger.LogError(formatString, args); - } + /// + public void Error(string formatString, params object[] args) + { + _logger.LogError(formatString, args); + } - /// - public void Error(string formatString, Exception exception) - { - _logger.LogError(formatString, exception); - } + /// + public void Error(string formatString, Exception exception) + { + _logger.LogError(formatString, exception); + } - /// - public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) - { - string message = JsonSerializer.Serialize(logEntryModel, options); + /// + public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) + { + string message = JsonSerializer.Serialize(logEntryModel, _options); - _logger.LogDebug("Admin[{IsAdmin}] {Message}", isAdminRequest, message); - } + _logger.LogDebug("Admin[{IsAdmin}] {Message}", isAdminRequest, message); } } \ No newline at end of file