From d336971b32401a7fd96233d983cb6662fe74c769 Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Wed, 29 Dec 2021 15:53:24 -0800 Subject: [PATCH 1/3] Add ConfigureAwait. --- tools/Custom/HttpMessageLogFormatter.cs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tools/Custom/HttpMessageLogFormatter.cs b/tools/Custom/HttpMessageLogFormatter.cs index 6c6cce78b48..68400fcce63 100644 --- a/tools/Custom/HttpMessageLogFormatter.cs +++ b/tools/Custom/HttpMessageLogFormatter.cs @@ -22,11 +22,13 @@ public static string GetHttpRequestLog(HttpRequestMessage request) if (request == null) return string.Empty; string body = string.Empty; - try - { - body = (request.Content == null) ? string.Empty : FormatString(request.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + if (request.Content != null) { + try + { + var content = request.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + body = FormatString(content); + } catch {} } - catch { } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"============================ HTTP REQUEST ============================{Environment.NewLine}"); @@ -42,11 +44,13 @@ public static string GetHttpResponseLog(HttpResponseMessage response) if (response == null) return string.Empty; string body = string.Empty; - try - { - body = (response.Content == null) ? string.Empty : FormatString(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + if (response.Content != null) { + try + { + var content = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + body = FormatString(content); + } catch {} } - catch { } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"============================ HTTP RESPONSE ============================{Environment.NewLine}"); From 38d1e48e0cb39e3da3793f6649ddb9423f78465a Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Thu, 24 Feb 2022 14:58:53 -0800 Subject: [PATCH 2/3] Fix empty body on -Debug --- tools/Custom/HttpMessageLogFormatter.cs | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/Custom/HttpMessageLogFormatter.cs b/tools/Custom/HttpMessageLogFormatter.cs index 68400fcce63..f3d261379ab 100644 --- a/tools/Custom/HttpMessageLogFormatter.cs +++ b/tools/Custom/HttpMessageLogFormatter.cs @@ -4,7 +4,6 @@ namespace Microsoft.Graph.PowerShell { - using Hyak.Common; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -22,12 +21,13 @@ public static string GetHttpRequestLog(HttpRequestMessage request) if (request == null) return string.Empty; string body = string.Empty; - if (request.Content != null) { + if (request.Content != null) + { try { - var content = request.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - body = FormatString(content); - } catch {} + body = (request.Content == null) ? string.Empty : FormatString(request.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + } + catch { } } StringBuilder stringBuilder = new StringBuilder(); @@ -44,13 +44,11 @@ public static string GetHttpResponseLog(HttpResponseMessage response) if (response == null) return string.Empty; string body = string.Empty; - if (response.Content != null) { - try - { - var content = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - body = FormatString(content); - } catch {} + try + { + body = (response.Content == null) ? string.Empty : FormatString(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); } + catch { } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"============================ HTTP RESPONSE ============================{Environment.NewLine}"); @@ -96,11 +94,13 @@ private static string FormatString(string content) { try { - if (CloudException.IsJson(content)) + content = content.Trim(); + if ((content.StartsWith("{") && content.EndsWith("}")) || // object + (content.StartsWith("[") && content.EndsWith("]"))) // array { return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(content), Formatting.Indented); } - if (CloudException.IsXml(content)) + if (content.StartsWith("<")) { return XDocument.Parse(content).ToString(); } From fcb9b1bbcf90095b6913ea562fa05f08b4f8184b Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Thu, 24 Feb 2022 15:12:52 -0800 Subject: [PATCH 3/3] Clean up code. --- tools/Custom/HttpMessageLogFormatter.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/Custom/HttpMessageLogFormatter.cs b/tools/Custom/HttpMessageLogFormatter.cs index f3d261379ab..797c5ab51f3 100644 --- a/tools/Custom/HttpMessageLogFormatter.cs +++ b/tools/Custom/HttpMessageLogFormatter.cs @@ -21,14 +21,11 @@ public static string GetHttpRequestLog(HttpRequestMessage request) if (request == null) return string.Empty; string body = string.Empty; - if (request.Content != null) + try { - try - { - body = (request.Content == null) ? string.Empty : FormatString(request.Content.ReadAsStringAsync().GetAwaiter().GetResult()); - } - catch { } + body = (request.Content == null) ? string.Empty : FormatString(request.Content.ReadAsStringAsync().GetAwaiter().GetResult()); } + catch { } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"============================ HTTP REQUEST ============================{Environment.NewLine}");