From 91c166dc4d501a922fb32148083012cad1031341 Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 19 May 2026 19:09:08 +0200 Subject: [PATCH] Add ILogger support for enhanced API call logging Introduced optional ILogger support across BaseCrudApi and its derived classes to enable logging of API call outcomes. Updated constructors to accept an optional ILogger parameter, with conditional compilation for .NET Framework compatibility. Replaced EnsureSuccessAsync with HandleResponseAsync in CRUD methods to integrate logging. Updated derived API classes (CommonApi, EndpointAuthApi, EndpointParamsApi, EndpointsApi, ProfileApi, RecActionApi, ResultApi) to pass ILogger to the base class. Added Microsoft.Extensions.Logging imports and ensured backward compatibility by making ILogger optional and handling nullable reference types in non-.NET Framework environments. --- src/ReC.Client/Api/BaseCrudApi.cs | 24 ++++++++++++++++++++---- src/ReC.Client/Api/CommonApi.cs | 8 +++++++- src/ReC.Client/Api/EndpointAuthApi.cs | 8 +++++++- src/ReC.Client/Api/EndpointParamsApi.cs | 8 +++++++- src/ReC.Client/Api/EndpointsApi.cs | 8 +++++++- src/ReC.Client/Api/ProfileApi.cs | 8 +++++++- src/ReC.Client/Api/RecActionApi.cs | 10 ++++++++-- src/ReC.Client/Api/ResultApi.cs | 8 +++++++- 8 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/ReC.Client/Api/BaseCrudApi.cs b/src/ReC.Client/Api/BaseCrudApi.cs index a6c4cbe..3728b11 100644 --- a/src/ReC.Client/Api/BaseCrudApi.cs +++ b/src/ReC.Client/Api/BaseCrudApi.cs @@ -2,6 +2,7 @@ using System; using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ReC.Client.Api { @@ -20,15 +21,30 @@ namespace ReC.Client.Api /// protected readonly string ResourcePath; + /// + /// An optional logger used to record API call outcomes. May be . + /// +#if NETFRAMEWORK + protected readonly ILogger Logger; +#else + protected readonly ILogger? Logger; +#endif + /// /// Initializes a new instance of the class. /// /// The HTTP client used for requests. /// The base resource path for the API endpoint. - protected BaseCrudApi(HttpClient http, string resourcePath) + /// An optional logger used to record API call outcomes. +#if NETFRAMEWORK + protected BaseCrudApi(HttpClient http, string resourcePath, ILogger logger = null) +#else + protected BaseCrudApi(HttpClient http, string resourcePath, ILogger? logger = null) +#endif { Http = http ?? throw new ArgumentNullException(nameof(http)); ResourcePath = resourcePath ?? throw new ArgumentNullException(nameof(resourcePath)); + Logger = logger; } /// @@ -43,7 +59,7 @@ namespace ReC.Client.Api using (var content = ReCClientHelpers.ToJsonContent(payload)) using (var resp = await Http.PostAsync(ResourcePath, content, cancel)) { - await ReCClientHelpers.EnsureSuccessAsync(resp, cancel).ConfigureAwait(false); + await ReCClientHelpers.HandleResponseAsync(resp, Logger, cancel).ConfigureAwait(false); } } @@ -60,7 +76,7 @@ namespace ReC.Client.Api using (var content = ReCClientHelpers.ToJsonContent(payload)) using (var resp = await Http.PutAsync($"{ResourcePath}/{id}", content, cancel)) { - await ReCClientHelpers.EnsureSuccessAsync(resp, cancel).ConfigureAwait(false); + await ReCClientHelpers.HandleResponseAsync(resp, Logger, cancel).ConfigureAwait(false); } } @@ -79,7 +95,7 @@ namespace ReC.Client.Api }) using (var resp = await Http.SendAsync(request, cancel)) { - await ReCClientHelpers.EnsureSuccessAsync(resp, cancel).ConfigureAwait(false); + await ReCClientHelpers.HandleResponseAsync(resp, Logger, cancel).ConfigureAwait(false); } } } diff --git a/src/ReC.Client/Api/CommonApi.cs b/src/ReC.Client/Api/CommonApi.cs index aea3cdf..d66b01b 100644 --- a/src/ReC.Client/Api/CommonApi.cs +++ b/src/ReC.Client/Api/CommonApi.cs @@ -1,6 +1,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ReC.Client.Api { @@ -13,7 +14,12 @@ namespace ReC.Client.Api /// Initializes a new instance of the class. /// /// The HTTP client used for requests. - public CommonApi(HttpClient http) : base(http, "api/Common") + /// An optional logger used to record API call outcomes. +#if NETFRAMEWORK + public CommonApi(HttpClient http, ILogger logger = null) : base(http, "api/Common", logger) +#else + public CommonApi(HttpClient http, ILogger? logger = null) : base(http, "api/Common", logger) +#endif { } } diff --git a/src/ReC.Client/Api/EndpointAuthApi.cs b/src/ReC.Client/Api/EndpointAuthApi.cs index 287d6d0..14fb041 100644 --- a/src/ReC.Client/Api/EndpointAuthApi.cs +++ b/src/ReC.Client/Api/EndpointAuthApi.cs @@ -1,6 +1,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ReC.Client.Api { @@ -13,7 +14,12 @@ namespace ReC.Client.Api /// Initializes a new instance of the class. /// /// The HTTP client used for requests. - public EndpointAuthApi(HttpClient http) : base(http, "api/EndpointAuth") + /// An optional logger used to record API call outcomes. +#if NETFRAMEWORK + public EndpointAuthApi(HttpClient http, ILogger logger = null) : base(http, "api/EndpointAuth", logger) +#else + public EndpointAuthApi(HttpClient http, ILogger? logger = null) : base(http, "api/EndpointAuth", logger) +#endif { } } diff --git a/src/ReC.Client/Api/EndpointParamsApi.cs b/src/ReC.Client/Api/EndpointParamsApi.cs index ac7b781..f02224b 100644 --- a/src/ReC.Client/Api/EndpointParamsApi.cs +++ b/src/ReC.Client/Api/EndpointParamsApi.cs @@ -1,6 +1,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ReC.Client.Api { @@ -13,7 +14,12 @@ namespace ReC.Client.Api /// Initializes a new instance of the class. /// /// The HTTP client used for requests. - public EndpointParamsApi(HttpClient http) : base(http, "api/EndpointParams") + /// An optional logger used to record API call outcomes. +#if NETFRAMEWORK + public EndpointParamsApi(HttpClient http, ILogger logger = null) : base(http, "api/EndpointParams", logger) +#else + public EndpointParamsApi(HttpClient http, ILogger? logger = null) : base(http, "api/EndpointParams", logger) +#endif { } } diff --git a/src/ReC.Client/Api/EndpointsApi.cs b/src/ReC.Client/Api/EndpointsApi.cs index cc8634e..0d70339 100644 --- a/src/ReC.Client/Api/EndpointsApi.cs +++ b/src/ReC.Client/Api/EndpointsApi.cs @@ -1,6 +1,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ReC.Client.Api { @@ -13,7 +14,12 @@ namespace ReC.Client.Api /// Initializes a new instance of the class. /// /// The HTTP client used for requests. - public EndpointsApi(HttpClient http) : base(http, "api/Endpoints") + /// An optional logger used to record API call outcomes. +#if NETFRAMEWORK + public EndpointsApi(HttpClient http, ILogger logger = null) : base(http, "api/Endpoints", logger) +#else + public EndpointsApi(HttpClient http, ILogger? logger = null) : base(http, "api/Endpoints", logger) +#endif { } } diff --git a/src/ReC.Client/Api/ProfileApi.cs b/src/ReC.Client/Api/ProfileApi.cs index a2f6827..ea174e5 100644 --- a/src/ReC.Client/Api/ProfileApi.cs +++ b/src/ReC.Client/Api/ProfileApi.cs @@ -1,6 +1,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ReC.Client.Api { @@ -13,7 +14,12 @@ namespace ReC.Client.Api /// Initializes a new instance of the class. /// /// The HTTP client used for requests. - public ProfileApi(HttpClient http) : base(http, "api/Profile") + /// An optional logger used to record API call outcomes. +#if NETFRAMEWORK + public ProfileApi(HttpClient http, ILogger logger = null) : base(http, "api/Profile", logger) +#else + public ProfileApi(HttpClient http, ILogger? logger = null) : base(http, "api/Profile", logger) +#endif { } diff --git a/src/ReC.Client/Api/RecActionApi.cs b/src/ReC.Client/Api/RecActionApi.cs index 53d9a20..6aed328 100644 --- a/src/ReC.Client/Api/RecActionApi.cs +++ b/src/ReC.Client/Api/RecActionApi.cs @@ -1,6 +1,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ReC.Client.Api { @@ -13,7 +14,12 @@ namespace ReC.Client.Api /// Initializes a new instance of the class. /// /// The HTTP client used for requests. - public RecActionApi(HttpClient http) : base(http, "api/RecAction") + /// An optional logger used to record API call outcomes. +#if NETFRAMEWORK + public RecActionApi(HttpClient http, ILogger logger = null) : base(http, "api/RecAction", logger) +#else + public RecActionApi(HttpClient http, ILogger? logger = null) : base(http, "api/RecAction", logger) +#endif { } @@ -30,7 +36,7 @@ namespace ReC.Client.Api using (content) using (var resp = await Http.PostAsync($"{ResourcePath}/invoke/{profileId}", content, cancellationToken)) { - await ReCClientHelpers.EnsureSuccessAsync(resp, cancellationToken).ConfigureAwait(false); + await ReCClientHelpers.HandleResponseAsync(resp, Logger, cancellationToken).ConfigureAwait(false); } } diff --git a/src/ReC.Client/Api/ResultApi.cs b/src/ReC.Client/Api/ResultApi.cs index 37dca07..abced0c 100644 --- a/src/ReC.Client/Api/ResultApi.cs +++ b/src/ReC.Client/Api/ResultApi.cs @@ -1,6 +1,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ReC.Client.Api { @@ -13,7 +14,12 @@ namespace ReC.Client.Api /// Initializes a new instance of the class. /// /// The HTTP client used for requests. - public ResultApi(HttpClient http) : base(http, "api/Result") + /// An optional logger used to record API call outcomes. +#if NETFRAMEWORK + public ResultApi(HttpClient http, ILogger logger = null) : base(http, "api/Result", logger) +#else + public ResultApi(HttpClient http, ILogger? logger = null) : base(http, "api/Result", logger) +#endif { }