Add WireMockNullLogger as valid commandline logger option (#845)

* Add WireMockNullLogger as valid commandline logger option

* .
This commit is contained in:
Stef Heyenrath
2022-11-06 13:25:26 +01:00
committed by GitHub
parent 49b29d74dc
commit 7fd1d30d0e
7 changed files with 190 additions and 189 deletions

View File

@@ -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");
}
}

View File

@@ -1,8 +1,8 @@
{
"profiles": {
"dotnet-WireMock.Net": {
"commandName": "Project",
"commandLineArgs": ""
"commandName": "Project",
"commandLineArgs": "--WireMockLogger s"
}
}
}

View File

@@ -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;
}
/// <see cref="IWireMockLogger.Debug"/>
public void Debug(string formatString, params object[] args)
{
_logger.LogDebug(formatString, args);
}
/// <see cref="IWireMockLogger.Debug"/>
public void Debug(string formatString, params object[] args)
{
_logger.LogDebug(formatString, args);
}
/// <see cref="IWireMockLogger.Info"/>
public void Info(string formatString, params object[] args)
{
_logger.LogInformation(formatString, args);
}
/// <see cref="IWireMockLogger.Info"/>
public void Info(string formatString, params object[] args)
{
_logger.LogInformation(formatString, args);
}
/// <see cref="IWireMockLogger.Warn"/>
public void Warn(string formatString, params object[] args)
{
_logger.LogWarning(formatString, args);
}
/// <see cref="IWireMockLogger.Warn"/>
public void Warn(string formatString, params object[] args)
{
_logger.LogWarning(formatString, args);
}
/// <see cref="IWireMockLogger.Error(string, object[])"/>
public void Error(string formatString, params object[] args)
{
_logger.LogError(formatString, args);
}
/// <see cref="IWireMockLogger.Error(string, object[])"/>
public void Error(string formatString, params object[] args)
{
_logger.LogError(formatString, args);
}
/// <see cref="IWireMockLogger.Error(string, Exception)"/>
public void Error(string formatString, Exception exception)
{
_logger.LogError(formatString, exception);
}
/// <see cref="IWireMockLogger.Error(string, Exception)"/>
public void Error(string formatString, Exception exception)
{
_logger.LogError(formatString, exception);
}
/// <see cref="IWireMockLogger.DebugRequestResponse"/>
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest)
{
string message = JsonSerializer.Serialize(logEntryModel, options);
/// <see cref="IWireMockLogger.DebugRequestResponse"/>
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);
}
}