Skip to content

Commit

Permalink
Minor update to OutboundHttpClientLogger.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
DrEsteban committed Nov 24, 2024
1 parent f0b37c0 commit ccc35b1
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/Support/OutboundHttpClientLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,45 @@ public class OutboundHttpClientLogger(ILogger<OutboundHttpClientLogger> _logger)
{
public void LogRequestFailed(object? context, HttpRequestMessage request, HttpResponseMessage? response, Exception exception, TimeSpan elapsed)
{
_logger.LogError(
exception,
"Request '{Request.Host}{Request.Path}' failed after {Response.ElapsedMilliseconds}ms w/ status '{Response.StatusCodeInt}'",
request.RequestUri?.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped),
request.RequestUri?.PathAndQuery,
elapsed.TotalMilliseconds.ToString("F1"),
(int?)response?.StatusCode);
(string? host, string? path) = GetRequestInfo(request);
string elapsedMsString = GetElapsedMsString(elapsed);

if (exception is OperationCanceledException)
{
_logger.LogWarning(
"Request '{Request.Host}{Request.Path}' was canceled after {Response.ElapsedMilliseconds}ms",
host,
path,
elapsedMsString);
}
else
{
_logger.LogError(
exception,
"Request '{Request.Method} {Request.Host}{Request.Path}' failed after {Response.ElapsedMilliseconds}ms w/ status '{Response.StatusCodeInt}'",
request.Method,
host,
path,
elapsedMsString,
(int?)response?.StatusCode);
}
}

public object? LogRequestStart(HttpRequestMessage request)
{
(string? host, string? path) = GetRequestInfo(request);
_logger.LogTrace(
"Sending '{Request.Method}' to '{Request.Host}{Request.Path}'",
request.Method,
request.RequestUri?.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped),
request.RequestUri?.PathAndQuery);
host,
path);
return null;
}

public void LogRequestStop(object? context, HttpRequestMessage request, HttpResponseMessage response, TimeSpan elapsed)
{
(string? host, string? path) = GetRequestInfo(request);
string elapsedMsString = GetElapsedMsString(elapsed);
var severity = response.IsSuccessStatusCode ? LogLevel.Debug : LogLevel.Information;
_logger.Log(
severity,
Expand All @@ -36,8 +54,15 @@ public void LogRequestStop(object? context, HttpRequestMessage request, HttpResp
(int)response.StatusCode,
response.StatusCode,
request.Method,
request.RequestUri?.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped),
request.RequestUri?.PathAndQuery,
elapsed.TotalMilliseconds.ToString("F1"));
host,
path,
elapsedMsString);
}
}

private static (string? host, string? path) GetRequestInfo(HttpRequestMessage request)
=> (request.RequestUri?.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped),
request.RequestUri?.PathAndQuery);

private static string GetElapsedMsString(TimeSpan elapsed)
=> elapsed.TotalMilliseconds.ToString("F1");
}

0 comments on commit ccc35b1

Please sign in to comment.