-
Notifications
You must be signed in to change notification settings - Fork 753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpClient logging based on new .NET 8 runtime APIs #4224
Conversation
/// <param name="exception">An optional <see cref="Exception"/> that was thrown within the outgoing HTTP request processing.</param> | ||
// TODO: adding a new parameter to the interface method is a breaking change. Pay additional attention to this during review. | ||
// Alternatively, we can decide to add another overload instead. | ||
void Enrich(IEnrichmentPropertyBag enrichmentBag, HttpRequestMessage request, HttpResponseMessage? response = null, Exception? exception = null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to discuss this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it breaking if it's defaulted to null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the breaking change here is that I added Exception? exception
- which means that existing users of this API would need to add the new parameter into their implementation of the interface
} | ||
|
||
// Recommendation is to swallow the exception (logger shouldn't throw), so we don't re-throw here: | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @CarnaViire pointed, a logger shouldn't re-throw (but it will change our current behavior)
|
||
public object? LogRequestStart(HttpRequestMessage request) | ||
{ | ||
throw new NotSupportedException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As of now the best we can offer for sync overloads - is to call their async counterparts with .GetAwaiter().GetResult()
, that's because there's no sync API of reading request/response message body
} | ||
|
||
propertyBag = LogMethodHelper.GetHelper(); | ||
FillLogRecord(logRecord, propertyBag, in elapsed, request, response, exception); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't use TimeProvider
here since elapsed
is provided out-of-the-box, but we can consider ignoring it and using the old logic
var httpMethod = request[startIndex].Value; | ||
var httpHost = request[startIndex + 1].Value; | ||
var httpPath = request[startIndex + 2].Value; | ||
return FormattableString.Invariant($"{httpMethod} {httpHost}/{httpPath}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to align with current behavior in internal library
src/Libraries/Microsoft.Extensions.Http.Telemetry/Logging/Internal/Log.cs
Outdated
Show resolved
Hide resolved
|
||
private static DelegatingHandler CreateInternalBclLoggingHandler(IHttpClientLogger logger, HttpMessageHandler? innerHandler) | ||
{ | ||
var handlerType = typeof(IHttpClientFactory).Assembly.GetType("Microsoft.Extensions.Http.Logging.HttpClientLoggerHandler"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was taken from Natalia's PoC branch main...CarnaViire:extensions:poc-hcf-custom-logging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure you need this? The intent on my side was just to show that our HttpClientLoggerHandler passes all the tests you had for your logging handler. Like, "you can trust our handler" 😄
So I believe the set of tests that were verifying the handler itself can be removed from dotnet/extensions repo (I haven't looked at the PR yet, I don't know how exactly you've transformed the test cases).
Or if you do need a handler for some other tests, create it "normal way" via IHttpMessageHandlerFactory.
...ibraries/Microsoft.Extensions.Http.Telemetry/Logging/Internal/LogRecordPooledObjectPolicy.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Http.Telemetry/Logging/Internal/Log.cs
Show resolved
Hide resolved
MC |
...braries/Microsoft.Extensions.Http.Telemetry/Latency/Internal/HttpClientLatencyLogEnricher.cs
Show resolved
Hide resolved
/// <param name="exception">An optional <see cref="Exception"/> that was thrown within the outgoing HTTP request processing.</param> | ||
// TODO: adding a new parameter to the interface method is a breaking change. Pay additional attention to this during review. | ||
// Alternatively, we can decide to add another overload instead. | ||
void Enrich(IEnrichmentPropertyBag enrichmentBag, HttpRequestMessage request, HttpResponseMessage? response = null, Exception? exception = null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it breaking if it's defaulted to null?
src/Libraries/Microsoft.Extensions.Http.Telemetry/Logging/IHttpClientLogEnricher.cs
Show resolved
Hide resolved
fix XML docs
use XUnit consistently in a test class
Please re-target to release/8.0, if this is necessary for .NET 8. |
I would rather merge it to |
remove orphaned file
MC |
Resolved |
* use new .NET 8 runtime APIs in HttpClient logging * Leverage KeyedServices feature in HttpClient logging (#4253) Co-authored-by: Nikita Balabaev <[email protected]>
* use new .NET 8 runtime APIs in HttpClient logging * Leverage KeyedServices feature in HttpClient logging (#4253) Co-authored-by: Nikita Balabaev <[email protected]>
Fixes #4211
Microsoft Reviewers: Open in CodeFlow