Skip to content

Commit

Permalink
Containers: LogHttpResponseAsync: include request method, and don't f…
Browse files Browse the repository at this point in the history
…ormat a message when trace logging is not enabled. (#35062)
  • Loading branch information
baronfel authored Aug 31, 2023
2 parents d2bcd36 + ca9efdd commit 23f2643
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ internal static bool IsAmazonECRRegistry(this Uri uri)
/// </summary>
internal static async Task LogHttpResponseAsync(this HttpResponseMessage response, ILogger logger, CancellationToken cancellationToken)
{
StringBuilder s = new();
s.AppendLine($"Last request URI: {response.RequestMessage?.RequestUri?.ToString()}");
s.AppendLine($"Status code: {response.StatusCode}");
s.AppendLine($"Response headers:");
s.AppendLine(response.Headers.ToString());
string detail = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
s.AppendLine($"Response content: {(string.IsNullOrWhiteSpace(detail) ? "<empty>" : detail)}");
logger.LogTrace(s.ToString());
if (logger.IsEnabled(LogLevel.Trace))
{
StringBuilder s = new();
s.AppendLine($"Request URI: {response.RequestMessage?.Method} {response.RequestMessage?.RequestUri?.ToString()}");
s.AppendLine($"Status code: {response.StatusCode}");
s.AppendLine($"Response headers:");
s.AppendLine(response.Headers.ToString());
string detail = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
s.AppendLine($"Response content: {(string.IsNullOrWhiteSpace(detail) ? "<empty>" : detail)}");
logger.LogTrace(s.ToString());
}
}
}

0 comments on commit 23f2643

Please sign in to comment.