Skip to content

Commit 50193fd

Browse files
[dotnet] Add more internal logs around CDP DevTools communication (#14558)
* [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]>
1 parent 0fe28e3 commit 50193fd

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

dotnet/src/webdriver/DevTools/DevToolsSession.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// limitations under the License.
1717
// </copyright>
1818

19+
using OpenQA.Selenium.Internal.Logging;
1920
using System;
2021
using System.Collections.Concurrent;
2122
using System.Globalization;
@@ -56,6 +57,8 @@ public class DevToolsSession : IDevToolsSession
5657
private DevToolsDomains domains;
5758
private readonly DevToolsOptions options;
5859

60+
private readonly static ILogger logger = Internal.Logging.Log.GetLogger<DevToolsSession>();
61+
5962
/// <summary>
6063
/// Initializes a new instance of the DevToolsSession class, using the specified WebSocket endpoint.
6164
/// </summary>
@@ -272,6 +275,11 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
272275

273276
if (this.connection != null && this.connection.IsActive)
274277
{
278+
if (logger.IsEnabled(LogEventLevel.Trace))
279+
{
280+
logger.Trace($"CDP SND >> {message.CommandId} {message.CommandName}: {commandParameters.ToJsonString()}");
281+
}
282+
275283
LogTrace("Sending {0} {1}: {2}", message.CommandId, message.CommandName, commandParameters.ToString());
276284

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

541549
private void ProcessMessage(string message)
542550
{
551+
if (logger.IsEnabled(LogEventLevel.Trace))
552+
{
553+
logger.Trace($"CDP RCV << {message}");
554+
}
555+
543556
var messageObject = JsonObject.Parse(message).AsObject();
544557

545558
if (messageObject.TryGetPropertyValue("id", out var idProperty))
@@ -583,7 +596,22 @@ private void ProcessMessage(string message)
583596
// DevTools commands that may be sent in the body of the attached
584597
// event handler. If thread pool starvation seems to become a problem,
585598
// we can switch to a channel-based queue.
586-
Task.Run(() => OnDevToolsEventReceived(new DevToolsEventReceivedEventArgs(methodParts[0], methodParts[1], eventData)));
599+
Task.Run(() =>
600+
{
601+
try
602+
{
603+
OnDevToolsEventReceived(new DevToolsEventReceivedEventArgs(methodParts[0], methodParts[1], eventData));
604+
}
605+
catch (Exception ex)
606+
{
607+
if (logger.IsEnabled(LogEventLevel.Warn))
608+
{
609+
logger.Warn($"CDP VNT ^^ Unhandled error occured in event handler of '{method}' method. {ex}");
610+
}
611+
612+
throw;
613+
}
614+
});
587615

588616
return;
589617
}

0 commit comments

Comments
 (0)