From 318ea75eb9054affdb943c984dafba84c805b2ce Mon Sep 17 00:00:00 2001 From: Scott Beddall <45376673+scbedd@users.noreply.github.com> Date: Thu, 26 Jan 2023 14:00:31 -0800 Subject: [PATCH] test-proxy always logs exceptions on console (#5218) * ensure that our logging always reveals httpexceptions and uncaught 500s on the running command line * apply feedback from Ben --- .../Common/DebugLogger.cs | 24 +++++++++++++++++++ .../Common/HttpExceptionMiddleware.cs | 7 ++++-- 2 files changed, 29 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..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,6 +78,30 @@ public static void LogInformation(string details) } } + public static void LogError(string details) + { + if (null != logger) + { + logger.LogError(details); + } + else + { + System.Console.WriteLine(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 86b3f04a512..8b567512be9 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, e); + var bodyObj = new { Message = e.Message,