Feature: Add logging of incoming request and body for tracability #109

Closed
opened 2025-12-29 08:22:24 +01:00 by adam · 2 comments
Owner

Originally created by @MartijnSips on GitHub (Jun 22, 2018).

When you have enabled the Consolelogger, it would be quite handy to log incomming requests (method, url and body) as well as the returning body if a mapping was found.

Originally created by @MartijnSips on GitHub (Jun 22, 2018). When you have enabled the Consolelogger, it would be quite handy to log incomming requests (method, url and body) as well as the returning body if a mapping was found.
adam closed this issue 2025-12-29 08:22:24 +01:00
Author
Owner

@MartijnSips commented on GitHub (Jun 22, 2018):

Example code for WireMockMiddleWare.cs line 56:

var bodyMessage = "";
if (!string.IsNullOrEmpty(request.Body))
{
    bodyMessage = $" with body [{request.Body}]";
}
_options.Logger.Info($"Incomming request: {request.Method.ToUpper()} {request.Url}{bodyMessage}");

and then you also need to modify the WireMockConsoleLogger.cs, namely the Format method:

private static string Format(string level, string formatString, params object[] args)
{
    string message = "";
    if (args.Length > 0)
    {
        message = string.Format(formatString, args);
    }
    else
    {
        message = formatString;
    }

    return $"{DateTime.UtcNow} [{level}] : {message}";
}
@MartijnSips commented on GitHub (Jun 22, 2018): Example code for WireMockMiddleWare.cs line 56: ``` var bodyMessage = ""; if (!string.IsNullOrEmpty(request.Body)) { bodyMessage = $" with body [{request.Body}]"; } _options.Logger.Info($"Incomming request: {request.Method.ToUpper()} {request.Url}{bodyMessage}"); ``` and then you also need to modify the WireMockConsoleLogger.cs, namely the Format method: ``` private static string Format(string level, string formatString, params object[] args) { string message = ""; if (args.Length > 0) { message = string.Format(formatString, args); } else { message = formatString; } return $"{DateTime.UtcNow} [{level}] : {message}"; } ```
Author
Owner

@StefH commented on GitHub (Jun 23, 2018):

I did extend the IWireMockLogger.cs with a new method. This is also implemented in the default WireMockConsoleLogger.cs. However if you want to control in more details how and what is logged, just create your own console logger.

@StefH commented on GitHub (Jun 23, 2018): I did extend the `IWireMockLogger.cs` with a new method. This is also implemented in the default `WireMockConsoleLogger.cs`. However if you want to control in more details how and what is logged, just create your own console logger.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#109