Skip to content
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

[dotnet] Log http requests/responses via internal DiagnosticsHttpHandler #13978

Merged
Merged
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a6c82f6
[dotnet] Add Execution Context
ChrstnAnsl May 20, 2024
9361903
Merge branch 'trunk' into dotnet-fix-no-such-execution-context
ChrstnAnsl May 21, 2024
abdd77c
Added http status code validation
ChrstnAnsl May 21, 2024
081b2b7
Merge branch 'dotnet-fix-no-such-execution-context' of https://github…
ChrstnAnsl May 21, 2024
535558a
update validation http status code validation as int
ChrstnAnsl May 21, 2024
ef9be50
Merge branch 'trunk' into dotnet-fix-no-such-execution-context
ChrstnAnsl May 21, 2024
ef1aeb2
Update http validation status code from 299 to 399
ChrstnAnsl May 22, 2024
9bf4018
Merge branch 'dotnet-fix-no-such-execution-context' of https://github…
ChrstnAnsl May 22, 2024
6b5b0d3
Logs Response to HttpClientHandler
ChrstnAnsl May 23, 2024
2cbc993
update name
ChrstnAnsl May 23, 2024
2c41350
revert http response message logger
ChrstnAnsl May 23, 2024
cd57a4e
Remove White Space
ChrstnAnsl May 23, 2024
62dc7a8
fix build error
ChrstnAnsl May 23, 2024
ace83aa
Update InterceptAsync Summary
ChrstnAnsl May 30, 2024
be96a83
Update Logger field initialization
ChrstnAnsl May 30, 2024
1b1421f
Remove logger parameter in ResponseLoggerInterceptor method
ChrstnAnsl May 30, 2024
62c70b8
fix comment to add responseBody inside the if statement
ChrstnAnsl May 31, 2024
a64a1fe
Update Delegating Handler and Remove unable to push files in local
ChrstnAnsl Jun 1, 2024
3b6a3ca
Update from null validation to status code
ChrstnAnsl Jun 1, 2024
4d42101
Fix comment
ChrstnAnsl Jun 21, 2024
4240536
fix minor comments and adjust logic improvement
ChrstnAnsl Jun 23, 2024
5c0822d
Revert "fix minor comments and adjust logic improvement"
nvborisenko Jun 23, 2024
30bd8f0
Null check for response content
nvborisenko Jun 23, 2024
d0ebc69
Merge remote-tracking branch 'upstream/trunk' into pr/13978
nvborisenko Jun 23, 2024
46db007
Requests/Responses as single log message
nvborisenko Jun 23, 2024
177291d
Pass logger from upstream
nvborisenko Jun 23, 2024
3817377
Fix missing responses in log
nvborisenko Jun 23, 2024
09de10c
Put log message in parallel with sending a request
nvborisenko Jun 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OpenQA.Selenium.Remote
Expand Down Expand Up @@ -237,7 +238,14 @@ private void CreateHttpClient()

httpClientHandler.Proxy = this.Proxy;

this.client = new HttpClient(httpClientHandler);
HttpMessageHandler handler = httpClientHandler;

if (_logger.IsEnabled(LogEventLevel.Trace))
{
handler = new DiagnosticsHttpHandler(httpClientHandler);
}

this.client = new HttpClient(handler);
this.client.DefaultRequestHeaders.UserAgent.ParseAdd(this.UserAgent);
this.client.DefaultRequestHeaders.Accept.ParseAdd(RequestAcceptHeader);
this.client.DefaultRequestHeaders.ExpectContinue = false;
Expand Down Expand Up @@ -385,5 +393,43 @@ private class HttpResponseInfo
public string Body { get; set; }
public string ContentType { get; set; }
}

/// <summary>
/// Internal Diagnostic Handler for HttpCommandExecutor Additional Context
/// </summary>
internal class DiagnosticsHttpHandler : DelegatingHandler
{
private static readonly ILogger _logger = Log.GetLogger<DiagnosticsHttpHandler>();

public DiagnosticsHttpHandler(HttpMessageHandler messageHandler)
: base(messageHandler)
{
}

/// <summary>
/// Sends the specified request and returns the associated response.
/// </summary>
/// <param name="request">The request to be sent.</param>
/// <param name="cancellationToken">A CancellationToken object to allow for cancellation of the request.</param>
/// <returns>The http response message content.</returns>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Content != null)
{
var requestContent = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
_logger.Trace($">> Body: {requestContent}");
}

var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved

if (!response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
_logger.Trace($"<< Body: {responseContent}");
}

return response;
}
}
}
}