-
Notifications
You must be signed in to change notification settings - Fork 17
feat(logging): separate fields #91
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
Merged
stephenlautier
merged 8 commits into
sketch7:master
from
neil-sciberras:feature/separate-logging-fields
Oct 11, 2024
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b734047
separated logging fields
neil-sciberras 0602477
added httpVersion and aligned other fields
neil-sciberras c221805
fixed method references
neil-sciberras 4c0a851
fix httpVersion field
neil-sciberras 61ed22e
remove path
neil-sciberras 50cb38d
use httpVersion from response
neil-sciberras f847381
move httpVersion in front
neil-sciberras b3af34b
update package version
neil-sciberras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -57,6 +57,9 @@ ILoggerFactory loggerFactory | |||||
public async Task<FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context) | ||||||
{ | ||||||
var request = context.Request; | ||||||
var path = request.Uri!.AbsolutePath; | ||||||
var httpVersion = request.Message.Version; | ||||||
|
||||||
if (!_logger.IsEnabled(LogLevel.Information)) | ||||||
return await _next(context); | ||||||
|
||||||
|
@@ -68,30 +71,30 @@ public async Task<FluentHttpResponse> Invoke(FluentHttpMiddlewareContext context | |||||
&& !options.ShouldLogDetailedResponse.GetValueOrDefault(false)) | ||||||
{ | ||||||
response = await _next(context); | ||||||
_logger.LoggerHttp_CondensedRequest(request.Method, request.Uri!, response.StatusCode, watch.GetElapsedTime().TotalMilliseconds); | ||||||
_logger.LoggerHttp_CondensedRequest(request.Method, request.Uri!, path, response.StatusCode, watch.GetElapsedTime().TotalMilliseconds, httpVersion); | ||||||
return response; | ||||||
} | ||||||
|
||||||
if (!(options.ShouldLogDetailedRequest ?? false)) | ||||||
_logger.LoggerHttp_Request(request); | ||||||
_logger.LoggerHttp_Request(request.Method, request.Uri!, path, httpVersion); | ||||||
else | ||||||
{ | ||||||
string? requestContent = null; | ||||||
if (request.Message.Content != null) | ||||||
requestContent = await request.Message.Content.ReadAsStringAsync(); | ||||||
_logger.LoggerHttp_RequestDetailed(request, request.Headers.ToFormattedString(), requestContent); | ||||||
_logger.LoggerHttp_RequestDetailed(request.Method, request.Uri!, path, request.Headers.ToFormattedString(), requestContent, httpVersion); | ||||||
} | ||||||
|
||||||
response = await _next(context); | ||||||
var stopwatchElapsed = watch.GetElapsedTime(); | ||||||
if (response.Content == null || !(options.ShouldLogDetailedResponse ?? false)) | ||||||
{ | ||||||
_logger.LoggerHttp_Response(response, stopwatchElapsed.TotalMilliseconds); | ||||||
_logger.LoggerHttp_Response(request.Method, request.Uri!, path, response.StatusCode, stopwatchElapsed.TotalMilliseconds, httpVersion); | ||||||
return response; | ||||||
} | ||||||
|
||||||
var responseContent = await response.Content.ReadAsStringAsync(); | ||||||
_logger.LoggerHttp_ResponseDetailed(response, response.Headers.ToFormattedString(), responseContent, stopwatchElapsed.TotalMilliseconds); | ||||||
_logger.LoggerHttp_ResponseDetailed(request.Method, request.Uri!, path, response.StatusCode, response.Headers.ToFormattedString(), responseContent, stopwatchElapsed.TotalMilliseconds, httpVersion); | ||||||
return response; | ||||||
} | ||||||
} | ||||||
|
@@ -166,18 +169,53 @@ public static FluentHttpClientBuilder UseLogging(this FluentHttpClientBuilder bu | |||||
|
||||||
internal static partial class LogExtensions | ||||||
{ | ||||||
[LoggerMessage(LogLevel.Information, "HTTP request [{method}] {requestUrl} responded {statusCode:D} in {elapsed:n0}ms")] | ||||||
internal static partial void LoggerHttp_CondensedRequest(this ILogger logger, HttpMethod method, Uri requestUrl, HttpStatusCode statusCode, double elapsed); | ||||||
|
||||||
[LoggerMessage(LogLevel.Information, "Pre - request... {request}")] | ||||||
internal static partial void LoggerHttp_Request(this ILogger logger, FluentHttpRequest request); | ||||||
|
||||||
[LoggerMessage(LogLevel.Information, "Pre-request... {request}\nHeaders: {headers}\nContent: {requestContent}")] | ||||||
internal static partial void LoggerHttp_RequestDetailed(this ILogger logger, FluentHttpRequest request, string headers, string? requestContent); | ||||||
|
||||||
[LoggerMessage(LogLevel.Information, "Post-request... {response} in {elapsed:n0}ms")] | ||||||
internal static partial void LoggerHttp_Response(this ILogger logger, FluentHttpResponse response, double elapsed); | ||||||
|
||||||
[LoggerMessage(LogLevel.Information, "Post-request... {response}\nHeaders: {headers}\nContent: {responseContent} in {elapsed:n0}ms")] | ||||||
internal static partial void LoggerHttp_ResponseDetailed(this ILogger logger, FluentHttpResponse response, string headers, string? responseContent, double elapsed); | ||||||
[LoggerMessage(LogLevel.Information, "HTTP request [{method}] '{requestUrl}' ({path}) responded {statusCode:D} in {elapsed:n0}ms ({httpVersion})")] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||
internal static partial void LoggerHttp_CondensedRequest( | ||||||
this ILogger logger, | ||||||
HttpMethod method, | ||||||
Uri requestUrl, | ||||||
string path, | ||||||
HttpStatusCode statusCode, | ||||||
double elapsed, | ||||||
Version httpVersion | ||||||
); | ||||||
|
||||||
[LoggerMessage(LogLevel.Information, "Pre - request... [{method}] '{requestUrl}' ({path}) ({httpVersion})")] | ||||||
internal static partial void LoggerHttp_Request(this ILogger logger, HttpMethod method, Uri requestUrl, string path, Version httpVersion); | ||||||
|
||||||
[LoggerMessage(LogLevel.Information, "Pre-request... [{method}] '{requestUrl}' ({path}) \nHeaders: {headers}\nContent: {requestContent} ({httpVersion})")] | ||||||
internal static partial void LoggerHttp_RequestDetailed( | ||||||
this ILogger logger, | ||||||
HttpMethod method, | ||||||
Uri requestUrl, | ||||||
string path, | ||||||
string headers, | ||||||
string? requestContent, | ||||||
Version httpVersion | ||||||
); | ||||||
|
||||||
[LoggerMessage(LogLevel.Information, "Post-request... [{method}] '{requestUrl}' ({path}) responded {statusCode:D} in {elapsed:n0}ms ({httpVersion})")] | ||||||
internal static partial void LoggerHttp_Response( | ||||||
this ILogger logger, | ||||||
HttpMethod method, | ||||||
Uri requestUrl, | ||||||
string path, | ||||||
HttpStatusCode statusCode, | ||||||
double elapsed, | ||||||
Version httpVersion | ||||||
); | ||||||
|
||||||
[LoggerMessage(LogLevel.Information, "Post-request... [{method}] '{requestUrl}' ({path}) responded {statusCode:D} ({httpVersion}) " + | ||||||
"\nHeaders: {headers}\nContent: {responseContent} in {elapsed:n0}ms")] | ||||||
internal static partial void LoggerHttp_ResponseDetailed( | ||||||
this ILogger logger, | ||||||
HttpMethod method, | ||||||
Uri requestUrl, | ||||||
string path, | ||||||
HttpStatusCode statusCode, | ||||||
string headers, | ||||||
string? responseContent, | ||||||
double elapsed, | ||||||
Version httpVersion | ||||||
); | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
httpVersion theres one in the request and one in the response - for this one we can log the response 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.
done