From 3e1ba6e578a87b36be36f1fc6ae2dd02e459194b Mon Sep 17 00:00:00 2001 From: scbedd <45376673+scbedd@users.noreply.github.com> Date: Wed, 25 Jan 2023 10:49:11 -0800 Subject: [PATCH 1/2] ensure that our logging always reveals httpexceptions and uncaught 500s on the running command line --- .../Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs | 12 ++++++++++++ .../Common/HttpExceptionMiddleware.cs | 7 +++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs index 8462aeea9f4..fe30e1f850a 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs @@ -79,6 +79,18 @@ public static void LogInformation(string details) } + public static void LogError(string details) + { + if (null != logger) + { + logger.LogError(details); + } + else + { + System.Console.WriteLine(details); + } + } + /// /// Simple access to the logging api. Accepts a simple message (preformatted) and logs to debug logger. /// diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs index 86b3f04a512..057d619e93c 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Text.Json; @@ -62,7 +63,7 @@ public async Task Invoke(HttpContext context) Status = e.StatusCode.ToString() }; - DebugLogger.LogInformation(e.Message); + DebugLogger.LogError(e.Message); var body = JsonSerializer.Serialize(bodyObj); await context.Response.WriteAsync(body); @@ -79,6 +80,8 @@ public async Task Invoke(HttpContext context) response.Headers.Add("x-request-exception", "true"); response.Headers.Add("x-request-exception-error", Convert.ToBase64String(Encoding.UTF8.GetBytes(e.Message))); + DebugLogger.LogError(unexpectedStatusCode.ToString() + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace); + var bodyObj = new { Message = e.Message, From 41987c7aa6469b4bc28fa13be2a2bfa7e80d23b1 Mon Sep 17 00:00:00 2001 From: scbedd <45376673+scbedd@users.noreply.github.com> Date: Thu, 26 Jan 2023 13:50:57 -0800 Subject: [PATCH 2/2] apply feedback from Ben --- .../Common/DebugLogger.cs | 14 +++++++++++++- .../Common/HttpExceptionMiddleware.cs | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs index fe30e1f850a..3ff9760b04e 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs @@ -78,7 +78,6 @@ public static void LogInformation(string details) } } - public static void LogError(string details) { if (null != logger) @@ -91,6 +90,19 @@ public static void LogError(string details) } } + public static void LogError(int statusCode, Exception e) + { + var details = statusCode.ToString() + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace; + if (null != logger) + { + logger.LogError(details); + } + else + { + System.Console.WriteLine(details); + } + } + /// /// Simple access to the logging api. Accepts a simple message (preformatted) and logs to debug logger. /// diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs index 057d619e93c..8b567512be9 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/HttpExceptionMiddleware.cs @@ -80,7 +80,7 @@ public async Task Invoke(HttpContext context) response.Headers.Add("x-request-exception", "true"); response.Headers.Add("x-request-exception-error", Convert.ToBase64String(Encoding.UTF8.GetBytes(e.Message))); - DebugLogger.LogError(unexpectedStatusCode.ToString() + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace); + DebugLogger.LogError(unexpectedStatusCode, e); var bodyObj = new {