Skip to content

Commit

Permalink
[dotnet] Add more internal logs around CDP DevTools communication (#1…
Browse files Browse the repository at this point in the history
…4558)

* [dotnet] Add more internal logs around CDP DevTools communications

* Warn only if this level is enabled

* Log error and throw

* One line cdp command params

---------

Co-authored-by: Puja Jagani <[email protected]>
  • Loading branch information
nvborisenko and pujagani authored Oct 4, 2024
1 parent 0fe28e3 commit 50193fd
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.
// </copyright>

using OpenQA.Selenium.Internal.Logging;
using System;
using System.Collections.Concurrent;
using System.Globalization;
Expand Down Expand Up @@ -56,6 +57,8 @@ public class DevToolsSession : IDevToolsSession
private DevToolsDomains domains;
private readonly DevToolsOptions options;

private readonly static ILogger logger = Internal.Logging.Log.GetLogger<DevToolsSession>();

/// <summary>
/// Initializes a new instance of the DevToolsSession class, using the specified WebSocket endpoint.
/// </summary>
Expand Down Expand Up @@ -272,6 +275,11 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains

if (this.connection != null && this.connection.IsActive)
{
if (logger.IsEnabled(LogEventLevel.Trace))
{
logger.Trace($"CDP SND >> {message.CommandId} {message.CommandName}: {commandParameters.ToJsonString()}");
}

LogTrace("Sending {0} {1}: {2}", message.CommandId, message.CommandName, commandParameters.ToString());

string contents = JsonSerializer.Serialize(message);
Expand Down Expand Up @@ -540,6 +548,11 @@ private void MonitorMessageQueue()

private void ProcessMessage(string message)
{
if (logger.IsEnabled(LogEventLevel.Trace))
{
logger.Trace($"CDP RCV << {message}");
}

var messageObject = JsonObject.Parse(message).AsObject();

if (messageObject.TryGetPropertyValue("id", out var idProperty))
Expand Down Expand Up @@ -583,7 +596,22 @@ private void ProcessMessage(string message)
// DevTools commands that may be sent in the body of the attached
// event handler. If thread pool starvation seems to become a problem,
// we can switch to a channel-based queue.
Task.Run(() => OnDevToolsEventReceived(new DevToolsEventReceivedEventArgs(methodParts[0], methodParts[1], eventData)));
Task.Run(() =>
{
try
{
OnDevToolsEventReceived(new DevToolsEventReceivedEventArgs(methodParts[0], methodParts[1], eventData));
}
catch (Exception ex)
{
if (logger.IsEnabled(LogEventLevel.Warn))
{
logger.Warn($"CDP VNT ^^ Unhandled error occured in event handler of '{method}' method. {ex}");
}

throw;
}
});

return;
}
Expand Down

0 comments on commit 50193fd

Please sign in to comment.