Skip to content

Commit

Permalink
test-proxy always logs exceptions on console (#5218)
Browse files Browse the repository at this point in the history
* ensure that our logging always reveals httpexceptions and uncaught 500s on the running command line
* apply feedback from Ben
scbedd authored Jan 26, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a2845e3 commit 318ea75
Showing 2 changed files with 29 additions and 2 deletions.
24 changes: 24 additions & 0 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

/// <summary>
/// Simple access to the logging api. Accepts a simple message (preformatted) and logs to debug logger.
Original file line number Diff line number Diff line change
@@ -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,

0 comments on commit 318ea75

Please sign in to comment.