using Newtonsoft.Json;
using System;
using WireMock.Admin.Requests;
namespace WireMock.Logging
{
///
/// WireMockConsoleLogger which logs to Console
///
///
public class WireMockConsoleLogger : IWireMockLogger
{
///
/// Initializes a new instance of the class.
///
public WireMockConsoleLogger()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
}
///
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 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, Exception exception)
{
Console.WriteLine(Format("Error", formatString, exception.Message));
if (exception is AggregateException ae)
{
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}";
}
}
}