Skip to content

Commit

Permalink
dump upstream request uri before sending
Browse files Browse the repository at this point in the history
  • Loading branch information
scbedd committed Jan 31, 2023
1 parent f633086 commit 0eb093c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/DebugLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public static async Task LogRequestDetailsAsync(ILogger loggerInstance, HttpRequ
}
}


/// <summary>
/// Helper function used to evaluate an incoming httprequest and non-destructively log some information about it using a provided logger instance. When not
/// actually logging anything, this function is entirely passthrough.
Expand Down Expand Up @@ -189,6 +190,19 @@ public static async Task LogRequestDetailsAsync(HttpRequest req)
}
}

/// <summary>
/// Helper function used to evaluate an incoming httprequest and non-destructively log some information about it using the non-DI logger instance. When not
/// actually logging anything, this function is entirely passthrough.
/// </summary>
/// <param name="req">The http request which needs to be detailed.</param>
/// <returns></returns>
public static async Task LogRequestDetailsAsync(HttpRequestMessage req)
{
if (CheckLogLevel(LogLevel.Debug))
{
logger.LogDebug(await _generateLogLine(req));
}
}

/// <summary>
/// Helper function used to evaluate an incoming httprequest and non-destructively log some information about it using the non-DI logger instance. When not
Expand All @@ -204,6 +218,30 @@ public static async Task LogResponseDetailsAsync(HttpResponseMessage resp)
}
}

/// <summary>
/// Generate a line of data from an http request. This is non-destructive, which means it does not mess
/// with the request Body stream at all.
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
private static async Task<string> _generateLogLine(HttpRequestMessage req)
{
StringBuilder sb = new StringBuilder();
string headers = string.Empty;

using (MemoryStream ms = new MemoryStream())
{
await JsonSerializer.SerializeAsync(ms, req.Headers);
headers = Encoding.UTF8.GetString(ms.ToArray());
}

sb.AppendLine("Request URI: [" + req.RequestUri+ "]");
sb.AppendLine("Request method: [" + req.Method + "]");
sb.AppendLine("Request headers: [" + headers + "]");

return sb.ToString();
}

/// <summary>
/// Generate a line of data from an http request. This is non-destructive, which means it does not mess
/// with the request Body stream at all.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ public async Task HandleRecordRequestAsync(string recordingId, HttpRequest incom

var upstreamRequest = CreateUpstreamRequest(incomingRequest, CompressionUtilities.CompressBody(entry.Request.Body, entry.Request.Headers));

await DebugLogger.LogRequestDetailsAsync(upstreamRequest);

HttpResponseMessage upstreamResponse = null;

// The experience around Content-Length is a bit weird in .NET. We're using the .NET native HttpClient class to send our requests. This comes with
Expand Down

0 comments on commit 0eb093c

Please sign in to comment.