From ba84b07713bb6f52926fee1891f96019981f07c0 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Mon, 10 Oct 2022 11:26:35 -0500 Subject: [PATCH] fix: Use compact JSON in debug logs When logging HTTP response/request bodies during communication with Radarr, Sonarr, etc, use a compact form instead. The previous form had newlines in it which ended up making the logs vertically very long and hard to follow. --- CHANGELOG.md | 5 +++++ src/TrashLib/Extensions/FlurlLogging.cs | 21 ++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85dcd25e..9c7b6aeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Docker: `PUID` and `PGID` no longer cause a failure on container start up. +### Fixed + +- Use compact JSON for HTTP request/response body in debug log files. This makes logs much easier to + scroll through. + ## [2.5.0] - 2022-09-11 ### Added diff --git a/src/TrashLib/Extensions/FlurlLogging.cs b/src/TrashLib/Extensions/FlurlLogging.cs index 7caacbfd..bdb34a52 100644 --- a/src/TrashLib/Extensions/FlurlLogging.cs +++ b/src/TrashLib/Extensions/FlurlLogging.cs @@ -1,5 +1,6 @@ using Flurl; using Flurl.Http.Configuration; +using Newtonsoft.Json; using Serilog; namespace TrashLib.Extensions; @@ -14,12 +15,7 @@ public static class FlurlLogging { var url = urlInterceptor(call.Request.Url.Clone()); log.Debug("HTTP Request: {Method} {Url}", call.HttpRequestMessage.Method, url); - - var body = call.RequestBody; - if (body is not null) - { - log.Debug("HTTP Body:\n{Body}", body); - } + LogBody(log, call.RequestBody); }; settings.AfterCallAsync = async call => @@ -31,7 +27,7 @@ public static class FlurlLogging var content = call.Response?.ResponseMessage.Content; if (content is not null) { - log.Debug("HTTP Body:\n{Body}", await content.ReadAsStringAsync()); + LogBody(log, await content.ReadAsStringAsync()); } }; @@ -44,6 +40,17 @@ public static class FlurlLogging }; } + private static void LogBody(ILogger log, string? body) + { + if (body is null) + { + return; + } + + body = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(body)); + log.Debug("HTTP Body: {Body}", body); + } + public static Url SanitizeUrl(Url url) { // Replace hostname and API key for user privacy