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 26 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
73 changes: 61 additions & 12 deletions dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
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 @@ -247,7 +247,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, _logger);
}

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 @@ -293,18 +300,8 @@ private async Task<HttpResponseInfo> MakeHttpRequest(HttpRequestInfo requestInfo
requestMessage.Content.Headers.ContentType = contentTypeHeader;
}

if (_logger.IsEnabled(LogEventLevel.Trace))
{
_logger.Trace($">> {requestMessage}");
}

using (HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage).ConfigureAwait(false))
{
if (_logger.IsEnabled(LogEventLevel.Trace))
{
_logger.Trace($"<< {responseMessage}");
}

HttpResponseInfo httpResponseInfo = new HttpResponseInfo();
httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
httpResponseInfo.ContentType = responseMessage.Content.Headers.ContentType?.ToString();
Expand Down Expand Up @@ -395,5 +392,57 @@ private class HttpResponseInfo
public string Body { get; set; }
public string ContentType { get; set; }
}

/// <summary>
/// Internal diagnostic handler to log http requests/responses.
/// </summary>
private class DiagnosticsHttpHandler : DelegatingHandler
{
private readonly ILogger _logger;

public DiagnosticsHttpHandler(HttpMessageHandler messageHandler, ILogger logger)
: base(messageHandler)
{
_logger = logger;
}

/// <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)
{
StringBuilder requestLogMessageBuilder = new();
requestLogMessageBuilder.AppendFormat(">> {0}", request);

if (request.Content != null)
{
var requestContent = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
requestLogMessageBuilder.AppendFormat("{0}{1}", Environment.NewLine, requestContent);
}

_logger.Trace(requestLogMessageBuilder.ToString());

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

if (!response.IsSuccessStatusCode)
{
StringBuilder responseLogMessageBuilder = new();
responseLogMessageBuilder.AppendFormat("<< {0}", response);

if (response.Content != null)
{
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
responseLogMessageBuilder.AppendFormat("{0}{1}", Environment.NewLine, responseContent);
}

_logger.Trace(responseLogMessageBuilder.ToString());
}

return response;
}
}
}
}
Loading