Skip to content

Commit

Permalink
Fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrstnAnsl committed Jun 21, 2024
1 parent 3b6a3ca commit 4d42101
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 61 deletions.
50 changes: 47 additions & 3 deletions 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,9 +238,14 @@ private void CreateHttpClient()

httpClientHandler.Proxy = this.Proxy;

// Use the custom LoggingHandler
var loggingHandler = new ResponseLoggerInterceptor(httpClientHandler);
this.client = new HttpClient(loggingHandler);
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 @@ -387,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);
_logger.Trace($">> Body: {requestContent}");
}

var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

if (!response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
_logger.Trace($"<< Body: {responseContent}");
}

return response;
}
}
}
}
58 changes: 0 additions & 58 deletions dotnet/src/webdriver/Remote/ResponseLoggerInterceptor.cs

This file was deleted.

0 comments on commit 4d42101

Please sign in to comment.