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
Show file tree
Hide file tree
Changes from 19 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
4 changes: 3 additions & 1 deletion dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ private void CreateHttpClient()

httpClientHandler.Proxy = this.Proxy;

this.client = new HttpClient(httpClientHandler);
// Use the custom LoggingHandler
var loggingHandler = new ResponseLoggerInterceptor(httpClientHandler);
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
this.client = new HttpClient(loggingHandler);
this.client.DefaultRequestHeaders.UserAgent.ParseAdd(this.UserAgent);
this.client.DefaultRequestHeaders.Accept.ParseAdd(RequestAcceptHeader);
this.client.DefaultRequestHeaders.ExpectContinue = false;
Expand Down
58 changes: 58 additions & 0 deletions dotnet/src/webdriver/Remote/ResponseLoggerInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// <copyright file="ResponseLoggerInterceptor.cs" company="WebDriver Committers">
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Net.Http;
using System.Threading.Tasks;
using OpenQA.Selenium.Internal.Logging;
using System;

namespace OpenQA.Selenium.Remote
{
/// <summary>
/// Interceptor for logging HTTP responses.
/// </summary>
public class ResponseLoggerInterceptor : DelegatingHandler
{
private static readonly ILogger _logger = Log.GetLogger<ResponseLoggerInterceptor>();

public ResponseLoggerInterceptor(HttpMessageHandler innerHandler)
: base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Content != null)
{
string requestContent = await request.Content.ReadAsStringAsync();
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
_logger.Trace($">> Body: {requestContent}");
}

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

if ((int)response.StatusCode < 200 || (int)response.StatusCode > 399)
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
{
string responseContent = await response.Content.ReadAsStringAsync();
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
_logger.Trace($"<< Body: {responseContent}");
}

return response;
}
}

}
Loading