diff --git a/examples/WireMock.Net.Service/WireMockLog4NetLogger.cs b/examples/WireMock.Net.Service/WireMockLog4NetLogger.cs index a1d7be2c..62d79d68 100644 --- a/examples/WireMock.Net.Service/WireMockLog4NetLogger.cs +++ b/examples/WireMock.Net.Service/WireMockLog4NetLogger.cs @@ -1,4 +1,5 @@ -using log4net; +using System; +using log4net; using Newtonsoft.Json; using Wiremock.Net.Service; using WireMock.Admin.Requests; @@ -30,6 +31,11 @@ namespace WireMock.Net.Service Log.ErrorFormat(formatString, args); } + public void Error(string message, Exception exception) + { + Log.Error(message, exception); + } + public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) { string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented); diff --git a/examples/WireMock.Net.StandAlone.NETCoreApp/WireMockLog4NetLogger.cs b/examples/WireMock.Net.StandAlone.NETCoreApp/WireMockLog4NetLogger.cs index 033894bb..a0ed6b15 100644 --- a/examples/WireMock.Net.StandAlone.NETCoreApp/WireMockLog4NetLogger.cs +++ b/examples/WireMock.Net.StandAlone.NETCoreApp/WireMockLog4NetLogger.cs @@ -1,7 +1,7 @@ -using log4net; +using System; +using log4net; using Newtonsoft.Json; using WireMock.Logging; -using WireMock.Models.Requests; namespace WireMock.Net.StandAlone.NETCoreApp { @@ -29,6 +29,11 @@ namespace WireMock.Net.StandAlone.NETCoreApp Log.ErrorFormat(formatString, args); } + public void Error(string message, Exception exception) + { + Log.Error(message, exception); + } + public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) { string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented); diff --git a/src/WireMock.Net.Abstractions/Logging/IWireMockLogger.cs b/src/WireMock.Net.Abstractions/Logging/IWireMockLogger.cs index 86a1a2d1..a1849813 100644 --- a/src/WireMock.Net.Abstractions/Logging/IWireMockLogger.cs +++ b/src/WireMock.Net.Abstractions/Logging/IWireMockLogger.cs @@ -1,4 +1,5 @@ -using JetBrains.Annotations; +using System; +using JetBrains.Annotations; using WireMock.Admin.Requests; namespace WireMock.Logging @@ -45,6 +46,14 @@ namespace WireMock.Logging [StringFormatMethod("formatString")] void Error([NotNull] string formatString, [NotNull] params object[] args); + /// + /// Writes the message at the Error level using the specified exception. + /// + /// The message. + /// The exception. + [PublicAPI] + void Error([NotNull] string message, [NotNull] Exception exception); + /// /// Writes the LogEntryModel (LogRequestModel, LogResponseModel and more). /// diff --git a/src/WireMock.Net/Logging/WireMockConsoleLogger.cs b/src/WireMock.Net/Logging/WireMockConsoleLogger.cs index 51dc8c83..fcb588c9 100644 --- a/src/WireMock.Net/Logging/WireMockConsoleLogger.cs +++ b/src/WireMock.Net/Logging/WireMockConsoleLogger.cs @@ -36,12 +36,27 @@ namespace WireMock.Logging 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}", exception.Message)); + return true; + }); + } + } + /// public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) { diff --git a/src/WireMock.Net/Logging/WireMockNullLogger.cs b/src/WireMock.Net/Logging/WireMockNullLogger.cs index f1627a79..8e55a69d 100644 --- a/src/WireMock.Net/Logging/WireMockNullLogger.cs +++ b/src/WireMock.Net/Logging/WireMockNullLogger.cs @@ -1,4 +1,5 @@ -using WireMock.Admin.Requests; +using System; +using WireMock.Admin.Requests; namespace WireMock.Logging { @@ -26,12 +27,18 @@ namespace WireMock.Logging // Log nothing } - /// + /// public void Error(string formatString, params object[] args) { // Log nothing } + /// + public void Error(string formatString, Exception exception) + { + // Log nothing + } + /// public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) { diff --git a/src/WireMock.Net/Owin/GlobalExceptionMiddleware.cs b/src/WireMock.Net/Owin/GlobalExceptionMiddleware.cs index 72ba8221..7aa03e71 100644 --- a/src/WireMock.Net/Owin/GlobalExceptionMiddleware.cs +++ b/src/WireMock.Net/Owin/GlobalExceptionMiddleware.cs @@ -63,7 +63,7 @@ namespace WireMock.Owin } catch (Exception ex) { - _options.Logger.Error("HttpStatusCode set to 500 {0}", ex); + _options.Logger.Error("HttpStatusCode set to 500", ex); await _responseMapper.MapAsync(ResponseMessageBuilder.Create(JsonConvert.SerializeObject(ex), 500), ctx.Response); } } diff --git a/src/WireMock.Net/Server/WireMockServer.Admin.cs b/src/WireMock.Net/Server/WireMockServer.Admin.cs index 73852d7a..8ad49169 100644 --- a/src/WireMock.Net/Server/WireMockServer.Admin.cs +++ b/src/WireMock.Net/Server/WireMockServer.Admin.cs @@ -490,12 +490,12 @@ namespace WireMock.Server } catch (ArgumentException a) { - _settings.Logger.Error("HttpStatusCode set to 400 {0}", a); + _settings.Logger.Error("HttpStatusCode set to 400", a); return ResponseMessageBuilder.Create(a.Message, 400); } catch (Exception e) { - _settings.Logger.Error("HttpStatusCode set to 500 {0}", e); + _settings.Logger.Error("HttpStatusCode set to 500", e); return ResponseMessageBuilder.Create(e.ToString(), 500); } }