HttpClientBuilderExtensions.AddLogger Method

Definition

Overloads

AddLogger(IHttpClientBuilder, Func<IServiceProvider,IHttpClientLogger>, Boolean)

Adds a delegate that will be used to create an additional logger for a named HttpClient. The custom logger would be invoked from a dedicated logging DelegatingHandler on every request of the corresponding named HttpClient.

AddLogger<TLogger>(IHttpClientBuilder, Boolean)

Adds a delegate that will be used to create an additional logger for a named HttpClient. The custom logger would be invoked from a dedicated logging DelegatingHandler on every request of the corresponding named HttpClient.

AddLogger(IHttpClientBuilder, Func<IServiceProvider,IHttpClientLogger>, Boolean)

Source:
HttpClientBuilderExtensions.Logging.cs
Source:
HttpClientBuilderExtensions.Logging.cs
Source:
HttpClientBuilderExtensions.Logging.cs

Adds a delegate that will be used to create an additional logger for a named HttpClient. The custom logger would be invoked from a dedicated logging DelegatingHandler on every request of the corresponding named HttpClient.

C#
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddLogger(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, Func<IServiceProvider,Microsoft.Extensions.Http.Logging.IHttpClientLogger> httpClientLoggerFactory, bool wrapHandlersPipeline = false);

Parameters

httpClientLoggerFactory
Func<IServiceProvider,IHttpClientLogger>

A delegate that is used to create a custom logger. The logger should implement IHttpClientLogger or IHttpClientAsyncLogger.

wrapHandlersPipeline
Boolean

Whether the logging handler with the custom logger would be added to the top or to the bottom of the additional handlers chains.

Returns

An IHttpClientBuilder that can be used to configure the client.

Remarks

If the wrapHandlersPipeline is `true`, LogRequestStart(HttpRequestMessage) and LogRequestStartAsync(HttpRequestMessage, CancellationToken) would be executed before all other additional handlers in the chain. LogRequestStop(Object, HttpRequestMessage, HttpResponseMessage, TimeSpan) and LogRequestStopAsync(Object, HttpRequestMessage, HttpResponseMessage, TimeSpan, CancellationToken) would be executed after all other additional handlers, essentially wrapping the whole pipeline.

If the wrapHandlersPipeline is `false`, LogRequestStart(HttpRequestMessage) and LogRequestStartAsync(HttpRequestMessage, CancellationToken) would be executed after all other additional handlers in the chain, right before the primary handler. LogRequestStop(Object, HttpRequestMessage, HttpResponseMessage, TimeSpan) and LogRequestStopAsync(Object, HttpRequestMessage, HttpResponseMessage, TimeSpan, CancellationToken) would be executed before all other additional handlers, right after the primary handler.

The IServiceProvider argument provided to httpClientLoggerFactory will be a reference to a scoped service provider that shares the lifetime of the handler chain being constructed.

If AddLogger(IHttpClientBuilder, Func<IServiceProvider,IHttpClientLogger>, Boolean) is called multiple times, multiple loggers would be added. If RemoveAllLoggers(IHttpClientBuilder) was not called before calling AddLogger(IHttpClientBuilder, Func<IServiceProvider,IHttpClientLogger>, Boolean), then new logger would be added in addition to the default ones.

Applies to

.NET 10 (package-provided) e outras versões
Produto Versões
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Standard 2.0 (package-provided)

AddLogger<TLogger>(IHttpClientBuilder, Boolean)

Source:
HttpClientBuilderExtensions.Logging.cs
Source:
HttpClientBuilderExtensions.Logging.cs
Source:
HttpClientBuilderExtensions.Logging.cs

Adds a delegate that will be used to create an additional logger for a named HttpClient. The custom logger would be invoked from a dedicated logging DelegatingHandler on every request of the corresponding named HttpClient.

C#
public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddLogger<TLogger>(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, bool wrapHandlersPipeline = false) where TLogger : Microsoft.Extensions.Http.Logging.IHttpClientLogger;

Type Parameters

TLogger

The service type of the custom logger as it was registered in DI. The logger should implement IHttpClientLogger or IHttpClientAsyncLogger.

Parameters

wrapHandlersPipeline
Boolean

Whether the logging handler with the custom logger would be added to the top or to the bottom of the additional handlers chains.

Returns

An IHttpClientBuilder that can be used to configure the client.

Remarks

If the wrapHandlersPipeline is `true`, LogRequestStart(HttpRequestMessage) and LogRequestStartAsync(HttpRequestMessage, CancellationToken) would be executed before all other additional handlers in the chain. LogRequestStop(Object, HttpRequestMessage, HttpResponseMessage, TimeSpan) and LogRequestStopAsync(Object, HttpRequestMessage, HttpResponseMessage, TimeSpan, CancellationToken) would be executed after all other additional handlers, essentially wrapping the whole pipeline.

If the wrapHandlersPipeline is `false`, LogRequestStart(HttpRequestMessage) and LogRequestStartAsync(HttpRequestMessage, CancellationToken) would be executed after all other additional handlers in the chain, right before the primary handler. LogRequestStop(Object, HttpRequestMessage, HttpResponseMessage, TimeSpan) and LogRequestStopAsync(Object, HttpRequestMessage, HttpResponseMessage, TimeSpan, CancellationToken) would be executed before all other additional handlers, right after the primary handler.

The TLogger will be resolved from a scoped service provider that shares the lifetime of the handler chain being constructed.

If AddLogger<TLogger>(IHttpClientBuilder, Boolean) is called multiple times, multiple loggers would be added. If RemoveAllLoggers(IHttpClientBuilder) was not called before calling AddLogger<TLogger>(IHttpClientBuilder, Boolean), then new logger would be added in addition to the default ones.

Applies to

.NET 10 (package-provided) e outras versões
Produto Versões
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Standard 2.0 (package-provided)