Add logging to HandleResponseAsync in ReCClientHelpers
Refactored the `EnsureSuccessAsync` method to `HandleResponseAsync` and added optional `ILogger` support for logging HTTP request and response details. - Added `using Microsoft.Extensions.Logging;` for logging. - Log success responses with HTTP method, URI, status code, and reason phrase. - Updated exception message construction for clarity. - Added conditional compilation for nullable `ILogger?` in non-NET Framework targets. - Improved code maintainability by consolidating logic.
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ReC.Client
|
||||
{
|
||||
@@ -46,14 +47,34 @@ namespace ReC.Client
|
||||
public static JsonContent ToJsonContent<T>(T payload) => JsonContent.Create(payload);
|
||||
|
||||
/// <summary>
|
||||
/// Throws a <see cref="ReCApiException"/> if the response indicates a non-success status code.
|
||||
/// Logs the outcome of an HTTP response. Throws a <see cref="ReCApiException"/> when the
|
||||
/// response indicates a non-success status code; otherwise writes an informational log entry
|
||||
/// containing the request and response details.
|
||||
/// </summary>
|
||||
/// <param name="response">The HTTP response to inspect.</param>
|
||||
/// <param name="logger">An optional logger used to record the outcome. May be <see langword="null"/>.</param>
|
||||
/// <param name="cancel">A token to cancel the operation.</param>
|
||||
public static async Task EnsureSuccessAsync(HttpResponseMessage response, CancellationToken cancel = default)
|
||||
#if NETFRAMEWORK
|
||||
public static async Task HandleResponseAsync(HttpResponseMessage response, ILogger logger = null, CancellationToken cancel = default)
|
||||
#else
|
||||
public static async Task HandleResponseAsync(HttpResponseMessage response, ILogger? logger = null, CancellationToken cancel = default)
|
||||
#endif
|
||||
{
|
||||
var request = response.RequestMessage;
|
||||
var method = request?.Method?.Method;
|
||||
var uri = request?.RequestUri;
|
||||
var statusCode = (int)response.StatusCode;
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
logger?.LogInformation(
|
||||
"ReC API request succeeded. {Method} {Uri} -> {StatusCode} ({ReasonPhrase})",
|
||||
method,
|
||||
uri,
|
||||
statusCode,
|
||||
response.ReasonPhrase);
|
||||
return;
|
||||
}
|
||||
|
||||
#if NETFRAMEWORK
|
||||
string body = null;
|
||||
@@ -76,11 +97,7 @@ namespace ReC.Client
|
||||
}
|
||||
}
|
||||
|
||||
var request = response.RequestMessage;
|
||||
var method = request?.Method?.Method;
|
||||
var uri = request?.RequestUri;
|
||||
|
||||
var message = $"ReC API request failed with status {(int)response.StatusCode} ({response.ReasonPhrase}). "
|
||||
var message = $"ReC API request failed with status {statusCode} ({response.ReasonPhrase}). "
|
||||
+ $"{method} {uri}"
|
||||
+ (string.IsNullOrWhiteSpace(body) ? string.Empty : $": {body}");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user