Add optional logging control to HandleResponseAsync
The HandleResponseAsync method was updated to include a new optional parameter, `logSuccess`, which allows control over whether successful HTTP responses are logged. The default value is `true`. This change applies to both `NETFRAMEWORK` and non-`NETFRAMEWORK` builds. The method's XML documentation was updated to reflect this new behavior.
This commit is contained in:
@@ -48,16 +48,17 @@ namespace ReC.Client
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Logs the outcome of an HTTP response. Throws a <see cref="ReCApiException"/> when the
|
/// 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
|
/// response indicates a non-success status code; otherwise (optionally) writes an informational
|
||||||
/// containing the request and response details.
|
/// log entry containing the request and response details.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="response">The HTTP response to inspect.</param>
|
/// <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="logger">An optional logger used to record the outcome. May be <see langword="null"/>.</param>
|
||||||
|
/// <param name="logSuccess">When <see langword="false"/>, successful responses are not logged.</param>
|
||||||
/// <param name="cancel">A token to cancel the operation.</param>
|
/// <param name="cancel">A token to cancel the operation.</param>
|
||||||
#if NETFRAMEWORK
|
#if NETFRAMEWORK
|
||||||
public static async Task HandleResponseAsync(HttpResponseMessage response, ILogger logger = null, CancellationToken cancel = default)
|
public static async Task HandleResponseAsync(HttpResponseMessage response, ILogger logger = null, bool logSuccess = true, CancellationToken cancel = default)
|
||||||
#else
|
#else
|
||||||
public static async Task HandleResponseAsync(HttpResponseMessage response, ILogger? logger = null, CancellationToken cancel = default)
|
public static async Task HandleResponseAsync(HttpResponseMessage response, ILogger? logger = null, bool logSuccess = true, CancellationToken cancel = default)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
var request = response.RequestMessage;
|
var request = response.RequestMessage;
|
||||||
@@ -66,6 +67,8 @@ namespace ReC.Client
|
|||||||
var statusCode = (int)response.StatusCode;
|
var statusCode = (int)response.StatusCode;
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode)
|
if (response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
if (logSuccess)
|
||||||
{
|
{
|
||||||
logger?.LogInformation(
|
logger?.LogInformation(
|
||||||
"ReC API request succeeded. {Method} {Uri} -> {StatusCode} ({ReasonPhrase})",
|
"ReC API request succeeded. {Method} {Uri} -> {StatusCode} ({ReasonPhrase})",
|
||||||
@@ -73,6 +76,7 @@ namespace ReC.Client
|
|||||||
uri,
|
uri,
|
||||||
statusCode,
|
statusCode,
|
||||||
response.ReasonPhrase);
|
response.ReasonPhrase);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user