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.​IHttpClientLogger
Assembly: Microsoft.Extensions.Http
An abstraction for 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>
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
object
LogRequestStart​(System.​Net.​Http.​HttpRequestMessage request)
Logs before sending an HTTP request.
Returns A context object that will be passed to a corresponding <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStop(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.TimeSpan)" /> or <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestFailed(System.Object,System.Net.Http.HttpRequestMessage,System.Net.Http.HttpResponseMessage,System.Exception,System.TimeSpan)" /> . Can be <see langword="null" />
if no context object is needed by the implementation.
request
The HTTP request message that will be sent.
void
LogRequestStop​(object context,
System.​Net.​Http.​HttpRequestMessage request,
System.​Net.​Http.​HttpResponseMessage response,
System.​TimeSpan elapsed)
Logs after receiving an HTTP response.
context
The context object that was previously returned by <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" /> .
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.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" /> .
void
LogRequestFailed​(object context,
System.​Net.​Http.​HttpRequestMessage request,
System.​Net.​Http.​HttpResponseMessage? response,
System.​Exception exception,
System.​TimeSpan elapsed)
Logs the exception happened while sending an HTTP request.
context
The context object that was previously returned by <see cref="M:Microsoft.Extensions.Http.Logging.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" /> .
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.IHttpClientLogger.LogRequestStart(System.Net.Http.HttpRequestMessage)" /> .