Validate logger presence for LogSuccessfulRequests

Added a validation in the `ReCClient` constructor to ensure that
an `ILogger` instance is provided when the `LogSuccessfulRequests`
option in `ReCClientOptions` is enabled. Throws an
`InvalidOperationException` with a detailed message if no logger
is injected. The message includes guidance on resolving the issue
by either registering a logging provider or disabling the option.
This commit is contained in:
2026-05-20 15:39:24 +02:00
parent 275746afde
commit 46eccf7a9b

View File

@@ -68,6 +68,13 @@ namespace ReC.Client
{ {
_http = httpClientFactory.CreateClient(ClientName); _http = httpClientFactory.CreateClient(ClientName);
var opts = options?.Value ?? new ReCClientOptions(); var opts = options?.Value ?? new ReCClientOptions();
if (opts.LogSuccessfulRequests && logger == null)
throw new InvalidOperationException(
$"{nameof(ReCClientOptions.LogSuccessfulRequests)} is enabled, but no {nameof(ILogger)} was injected into {nameof(ReCClient)}. " +
$"Register a logging provider (e.g. services.AddLogging()) so that an {nameof(ILogger)} can be resolved, " +
$"or set {nameof(ReCClientOptions.LogSuccessfulRequests)} to false.");
RecActions = new RecActionApi(_http, logger, opts); RecActions = new RecActionApi(_http, logger, opts);
Results = new ResultApi(_http, logger, opts); Results = new ResultApi(_http, logger, opts);
Profiles = new ProfileApi(_http, logger, opts); Profiles = new ProfileApi(_http, logger, opts);