Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logging an Exception Message (linux docker on azure) #405

Merged
merged 1 commit into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/WireMock.Net.Service/WireMockLog4NetLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using log4net;
using System;
using log4net;
using Newtonsoft.Json;
using Wiremock.Net.Service;
using WireMock.Admin.Requests;
Expand Down Expand Up @@ -30,6 +31,11 @@ public void Error(string formatString, params object[] args)
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -29,6 +29,11 @@ public void Error(string formatString, params object[] args)
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);
Expand Down
11 changes: 10 additions & 1 deletion src/WireMock.Net.Abstractions/Logging/IWireMockLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System;
using JetBrains.Annotations;
using WireMock.Admin.Requests;

namespace WireMock.Logging
Expand Down Expand Up @@ -45,6 +46,14 @@ public interface IWireMockLogger
[StringFormatMethod("formatString")]
void Error([NotNull] string formatString, [NotNull] params object[] args);

/// <summary>
/// Writes the message at the Error level using the specified exception.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="exception">The exception.</param>
[PublicAPI]
void Error([NotNull] string message, [NotNull] Exception exception);

/// <summary>
/// Writes the LogEntryModel (LogRequestModel, LogResponseModel and more).
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion src/WireMock.Net/Logging/WireMockConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,27 @@ public void Warn(string formatString, params object[] args)
Console.WriteLine(Format("Warn", formatString, args));
}

/// <see cref="IWireMockLogger.Error"/>
/// <see cref="IWireMockLogger.Error(string, object[])"/>
public void Error(string formatString, params object[] args)
{
Console.WriteLine(Format("Error", formatString, args));
}

/// <see cref="IWireMockLogger.Error(string, Exception)"/>
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;
});
}
}

/// <see cref="IWireMockLogger.DebugRequestResponse"/>
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest)
{
Expand Down
11 changes: 9 additions & 2 deletions src/WireMock.Net/Logging/WireMockNullLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using WireMock.Admin.Requests;
using System;
using WireMock.Admin.Requests;

namespace WireMock.Logging
{
Expand Down Expand Up @@ -26,12 +27,18 @@ public void Warn(string formatString, params object[] args)
// Log nothing
}

/// <see cref="IWireMockLogger.Error"/>
/// <see cref="IWireMockLogger.Error(string, object[])"/>
public void Error(string formatString, params object[] args)
{
// Log nothing
}

/// <see cref="IWireMockLogger.Error(string, Exception)"/>
public void Error(string formatString, Exception exception)
{
// Log nothing
}

/// <see cref="IWireMockLogger.DebugRequestResponse"/>
public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/Owin/GlobalExceptionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private async Task InvokeInternal(IContext ctx)
}
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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/WireMock.Net/Server/WireMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,12 @@ private ResponseMessage MappingsPost(RequestMessage requestMessage)
}
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);
}
}
Expand Down