From 0397c2246c156c4540a68ebd4ef7058a63f6d3b9 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:44:13 +0300 Subject: [PATCH 1/4] [dotnet] Add more internal logs around CDP DevTools communications --- .../src/webdriver/DevTools/DevToolsSession.cs | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsSession.cs b/dotnet/src/webdriver/DevTools/DevToolsSession.cs index 860074ecd2848..63e4780a4047e 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsSession.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsSession.cs @@ -16,6 +16,7 @@ // limitations under the License. // +using OpenQA.Selenium.Internal.Logging; using System; using System.Collections.Concurrent; using System.Globalization; @@ -56,6 +57,8 @@ public class DevToolsSession : IDevToolsSession private DevToolsDomains domains; private readonly DevToolsOptions options; + private readonly static ILogger logger = Internal.Logging.Log.GetLogger(); + /// /// Initializes a new instance of the DevToolsSession class, using the specified WebSocket endpoint. /// @@ -272,6 +275,11 @@ public T GetVersionSpecificDomains() where T : DevToolsSessionDomains if (this.connection != null && this.connection.IsActive) { + if (logger.IsEnabled(LogEventLevel.Trace)) + { + logger.Trace($"CDP SND >> {message.CommandId} {message.CommandName}: {commandParameters}"); + } + LogTrace("Sending {0} {1}: {2}", message.CommandId, message.CommandName, commandParameters.ToString()); string contents = JsonSerializer.Serialize(message); @@ -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)) @@ -583,7 +596,18 @@ 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) + { + logger.Warn($"CDP VNT ^^ Unhandled error occured in event handler of '{method}' method. {ex}"); + } + + }); return; } From 1e26ab19686186e1ead0dc2d5a8a52cceb6eb17e Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:50:04 +0300 Subject: [PATCH 2/4] Warn only if this level is enabled --- dotnet/src/webdriver/DevTools/DevToolsSession.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsSession.cs b/dotnet/src/webdriver/DevTools/DevToolsSession.cs index 63e4780a4047e..a2362bc58ac34 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsSession.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsSession.cs @@ -604,7 +604,10 @@ private void ProcessMessage(string message) } catch (Exception ex) { - logger.Warn($"CDP VNT ^^ Unhandled error occured in event handler of '{method}' method. {ex}"); + if (logger.IsEnabled(LogEventLevel.Warn)) + { + logger.Warn($"CDP VNT ^^ Unhandled error occured in event handler of '{method}' method. {ex}"); + } } }); From 23c320023468f60b16eeed2a7a494e26b54d554e Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:54:13 +0300 Subject: [PATCH 3/4] Log error and throw --- dotnet/src/webdriver/DevTools/DevToolsSession.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsSession.cs b/dotnet/src/webdriver/DevTools/DevToolsSession.cs index a2362bc58ac34..e5eae980e06aa 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsSession.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsSession.cs @@ -608,8 +608,9 @@ private void ProcessMessage(string message) { logger.Warn($"CDP VNT ^^ Unhandled error occured in event handler of '{method}' method. {ex}"); } - } + throw; + } }); return; From 8c7cbc2f0b2b5de8e7598a57821e6bfebb8604dd Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Thu, 3 Oct 2024 23:02:06 +0300 Subject: [PATCH 4/4] One line cdp command params --- dotnet/src/webdriver/DevTools/DevToolsSession.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/DevTools/DevToolsSession.cs b/dotnet/src/webdriver/DevTools/DevToolsSession.cs index e5eae980e06aa..931fb22dce019 100644 --- a/dotnet/src/webdriver/DevTools/DevToolsSession.cs +++ b/dotnet/src/webdriver/DevTools/DevToolsSession.cs @@ -277,7 +277,7 @@ public T GetVersionSpecificDomains() where T : DevToolsSessionDomains { if (logger.IsEnabled(LogEventLevel.Trace)) { - logger.Trace($"CDP SND >> {message.CommandId} {message.CommandName}: {commandParameters}"); + logger.Trace($"CDP SND >> {message.CommandId} {message.CommandName}: {commandParameters.ToJsonString()}"); } LogTrace("Sending {0} {1}: {2}", message.CommandId, message.CommandName, commandParameters.ToString());