Sdílet prostřednictvím


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.

public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddLogger (this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, Func<IServiceProvider,Microsoft.Extensions.Http.Logging.IHttpClientLogger> httpClientLoggerFactory, bool wrapHandlersPipeline = false);
static member AddLogger : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder * Func<IServiceProvider, Microsoft.Extensions.Http.Logging.IHttpClientLogger> * bool -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddLogger (builder As IHttpClientBuilder, httpClientLoggerFactory As Func(Of IServiceProvider, IHttpClientLogger), Optional wrapHandlersPipeline As Boolean = false) As IHttpClientBuilder

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

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.

public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddLogger<TLogger> (this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, bool wrapHandlersPipeline = false) where TLogger : Microsoft.Extensions.Http.Logging.IHttpClientLogger;
static member AddLogger : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder * bool -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder (requires 'Logger :> Microsoft.Extensions.Http.Logging.IHttpClientLogger)
<Extension()>
Public Function AddLogger(Of TLogger As IHttpClientLogger) (builder As IHttpClientBuilder, Optional wrapHandlersPipeline As Boolean = false) As IHttpClientBuilder

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