Early Preview

This is currently very much a preview. Please feel free to try things out, but don't be upset if anything is not yet working. Feedback is welcome over on our GitHub Dicussions page.

interface Microsoft.​Extensions.​Http.​Logging.​IHttpClientAsyncLogger

Assembly: Microsoft.Extensions.Http

Implemented Interfaces

An abstraction for asyncronous custom HTTP request logging for a named <see cref="T:System.Net.Http.HttpClient" /> instances returned by <see cref="T:System.Net.Http.IHttpClientFactory" /> .

Remarks

<para> Asyncronous methods (such as <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" />) would be called from async code paths (such as <see cref="M:System.Net.Http.HttpClient.SendAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" />), and their syncronous counterparts inherited from <see cref="T:Microsoft.Extensions.Http.Logging.IHttpClientLogger" /> (such as <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" />) would be called from the corresponding sync code paths. </para> <para> It is up to the user implementing the interface to decide whether to use <see cref="T:Microsoft.Extensions.Logging.ILogger" />, or anything else, and what exactly to log. However, the implementation should be mindful about potential adverse side effects of accessing some of the <see cref="T:System.Net.Http.HttpRequestMessage" /> or <see cref="T:System.Net.Http.HttpResponseMessage" /> properties, such as reading from a content stream; if possible, such behavior should be avoided. </para> <para> Logging implementation also should not throw any exceptions, as an unhandled exception in logging would fail the request. </para>

Methods

LogRequestStartAsync​(System.​Net.​Http.​HttpRequestMessage request, System.​Threading.​CancellationToken cancellationToken = null)
Logs before sending an HTTP request.
Returns The task object representing the asynchronous operation. The result of the operation is a context object that will be passed to a corresponding <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStopAsync(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan,System.Threading.CancellationToken)" /> or <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestFailedAsync(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.Exception,System.TimeSpan,System.Threading.CancellationToken)" /> . Can be <see langword="null" /> if no context object is needed by the implementation.
request The HTTP request message that will be sent.
cancellationToken The cancellation token to cancel operation.
LogRequestStopAsync​(object context, System.​Net.​Http.​HttpRequestMessage request, System.​Net.​Http.​HttpResponseMessage response, System.​TimeSpan elapsed, System.​Threading.​CancellationToken cancellationToken = null)
Logs after receiving an HTTP response.
Returns The task object representing the asynchronous operation.
context The context object that was previously returned by <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" /> .
request The HTTP request message that was sent.
response The HTTP response message that was received.
elapsed Time elapsed since calling <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" /> .
cancellationToken The cancellation token to cancel operation.
LogRequestFailedAsync​(object context, System.​Net.​Http.​HttpRequestMessage request, System.​Net.​Http.​HttpResponseMessage? response, System.​Exception exception, System.​TimeSpan elapsed, System.​Threading.​CancellationToken cancellationToken = null)
Logs the exception happened while sending an HTTP request.
Returns The task object representing the asynchronous operation.
context The context object that was previously returned by <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" /> .
request The HTTP request message that was sent.
response If available, the HTTP response message that was received, and <see langword="null" /> otherwise.
exception Exception that happened during processing the HTTP request.
elapsed Time elapsed since calling <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientAsyncLogger.LogRequestStartAsync(System.Net.Http.HttpRequestMessage,System.Threading.CancellationToken)" /> .
cancellationToken The cancellation token to cancel operation.