Skip to content

Commit

Permalink
Add support for showing timestamps on each intercepted request. Closes
Browse files Browse the repository at this point in the history
…microsoft#953 (microsoft#958)

* Add support for showing timestamps on each intercepted request. Closes microsoft#953

* Resolve PR comments

* Fix console color for Timestamp message type in ProxyConsoleFormatter
  • Loading branch information
garrytrinder authored Dec 13, 2024
1 parent ea26b98 commit 18e20cb
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions dev-proxy-abstractions/IProxyConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public interface IProxyConfiguration
bool Record { get; }
IEnumerable<int> WatchPids { get; }
IEnumerable<string> WatchProcessNames { get; }
bool ShowTimestamps { get; }
}
3 changes: 2 additions & 1 deletion dev-proxy-abstractions/IProxyLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public enum MessageType
InterceptedResponse,
FinishedProcessingRequest,
Skipped,
Processed
Processed,
Timestamp
}

public class LoggingContext(SessionEventArgs session)
Expand Down
6 changes: 5 additions & 1 deletion dev-proxy/Logging/ProxyConsoleFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ private void LogRequest(RequestLog requestLog, string category, IExternalScopePr

// don't log intercepted response to console
if (messageType == MessageType.InterceptedResponse ||
(messageType == MessageType.Skipped && !_options.ShowSkipMessages))
(messageType == MessageType.Skipped && !_options.ShowSkipMessages) ||
(messageType == MessageType.Timestamp && !_options.ShowTimestamps)
)
{
return;
}
Expand Down Expand Up @@ -238,6 +240,7 @@ private static string GetMessageTypeString(MessageType messageType)
MessageType.Tip => "tip",
MessageType.Skipped => "skip",
MessageType.Processed => "proc",
MessageType.Timestamp => "time",
_ => " "
};
}
Expand All @@ -258,6 +261,7 @@ private static (ConsoleColor bg, ConsoleColor fg) GetMessageTypeColor(MessageTyp
MessageType.Mocked => (ConsoleColor.DarkMagenta, fgColor),
MessageType.Failed => (ConsoleColor.DarkRed, fgColor),
MessageType.Tip => (ConsoleColor.DarkBlue, fgColor),
MessageType.Timestamp => (bgColor, ConsoleColor.Gray),
_ => (bgColor, fgColor)
};
}
Expand Down
2 changes: 2 additions & 0 deletions dev-proxy/Logging/ProxyConsoleFormatterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ namespace Microsoft.DevProxy.Logging;
public class ProxyConsoleFormatterOptions: ConsoleFormatterOptions
{
public bool ShowSkipMessages { get; set; } = true;

public bool ShowTimestamps { get; set; } = true;
}
1 change: 1 addition & 0 deletions dev-proxy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{
options.IncludeScopes = true;
options.ShowSkipMessages = ProxyCommandHandler.Configuration.ShowSkipMessages;
options.ShowTimestamps = ProxyCommandHandler.Configuration.ShowTimestamps;
})
.AddRequestLogger(pluginEvents)
.SetMinimumLevel(ProxyHost.LogLevel ?? ProxyCommandHandler.Configuration.LogLevel);
Expand Down
1 change: 1 addition & 0 deletions dev-proxy/ProxyConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ public class ProxyConfiguration : IProxyConfiguration
public MockRequestHeader[]? FilterByHeaders { get; set; }
public int ApiPort { get; set; } = 8897;
public bool ShowSkipMessages { get; set; } = true;
public bool ShowTimestamps { get; set; } = true;
}
4 changes: 4 additions & 0 deletions dev-proxy/ProxyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,11 @@ async Task OnRequestAsync(object sender, SessionEventArgs e)
using var scope = _logger.BeginScope(e.HttpClient.Request.Method ?? "", e.HttpClient.Request.Url, e.GetHashCode());

e.UserData = e.HttpClient.Request;

_logger.LogRequest($"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url}", MessageType.InterceptedRequest, new LoggingContext(e));

_logger.LogRequest($"{DateTimeOffset.UtcNow}", MessageType.Timestamp, new LoggingContext(e));

await HandleRequestAsync(e, proxyRequestArgs);
}
}
Expand Down
3 changes: 2 additions & 1 deletion dev-proxy/devproxyrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"rate": 50,
"logLevel": "information",
"newVersionNotification": "stable",
"showSkipMessages": true
"showSkipMessages": true,
"showTimestamps": true
}
5 changes: 4 additions & 1 deletion schemas/v0.24.0/rc.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "object",
"properties": {
"$schema": {
"type":"string"
"type": "string"
},
"apiPort": {
"type": "number",
Expand Down Expand Up @@ -101,6 +101,9 @@
"items": {
"type": "string"
}
},
"showTimestamps": {
"type": "boolean"
}
},
"required": [
Expand Down

0 comments on commit 18e20cb

Please sign in to comment.