mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-02-22 00:37:50 +01:00
* Add exception message to logging when mapping fails due to an exception.
* Revert "Add exception message to logging when mapping fails due to an exception."
This reverts commit eb7cf46c95.
* Fix loggers with improved exception logging.
80 lines
2.4 KiB
C#
80 lines
2.4 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
using Stef.Validation;
|
|
using TUnit.Core.Logging;
|
|
using WireMock.Admin.Requests;
|
|
using WireMock.Logging;
|
|
|
|
namespace WireMock.Net.TUnit;
|
|
|
|
/// <summary>
|
|
/// When using TUnit, this class enables to log the output from WireMock.Net to the <see cref="TUnitLogger"/>.
|
|
/// </summary>
|
|
// ReSharper disable once InconsistentNaming
|
|
public sealed class TUnitWireMockLogger : IWireMockLogger
|
|
{
|
|
private readonly TUnitLogger _tUnitLogger;
|
|
|
|
/// <summary>
|
|
/// Create a new instance on the <see cref="TUnitWireMockLogger"/>.
|
|
/// </summary>
|
|
/// <param name="tUnitLogger">Represents a class which can be used to provide test output.</param>
|
|
public TUnitWireMockLogger(TUnitLogger tUnitLogger)
|
|
{
|
|
_tUnitLogger = Guard.NotNull(tUnitLogger);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Debug(string formatString, params object[] args)
|
|
{
|
|
_tUnitLogger.LogDebug(Format("Debug", formatString, args));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Info(string formatString, params object[] args)
|
|
{
|
|
_tUnitLogger.LogInformation(Format("Info", formatString, args));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Warn(string formatString, params object[] args)
|
|
{
|
|
_tUnitLogger.LogWarning(Format("Warning", formatString, args));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Error(string formatString, params object[] args)
|
|
{
|
|
_tUnitLogger.LogError(Format("Error", formatString, args));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Error(string message, Exception exception)
|
|
{
|
|
_tUnitLogger.LogError(Format("Error", $"{message} {{0}}", exception));
|
|
|
|
if (exception is AggregateException ae)
|
|
{
|
|
ae.Handle(ex =>
|
|
{
|
|
_tUnitLogger.LogError(Format("Error", "Exception {0}", ex.Message), exception);
|
|
return true;
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest)
|
|
{
|
|
var message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented);
|
|
_tUnitLogger.LogDebug(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}";
|
|
}
|
|
} |