diff --git a/dotnet/src/webdriver/DevTools/DevToolsSession.cs b/dotnet/src/webdriver/DevTools/DevToolsSession.cs index 860074ecd2848..931fb22dce019 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.ToJsonString()}"); + } + 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,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; }