Skip to content

Commit 2e27142

Browse files
committed
WriteDriverLogToConsole
Internal variable to avoid using the stream when logs are sent to a file. Not exposed to the user.
1 parent 49701a6 commit 2e27142

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

dotnet/src/webdriver/DriverService.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,10 @@ public int ProcessId
155155
public string? DriverServicePath { get; set; }
156156

157157
/// <summary>
158-
/// Gets or sets a value indicating whether the driver process console output should be logged
159-
/// to the console. Defaults to <see langword="false"/>, meaning console output from the
160-
/// driver will be suppressed.
158+
/// Collects the driver log output and writes it to the console. Defaults to <see langword="true"/>.
159+
/// Internal variable to avoid using the stream when logs are sent to a file.
161160
/// </summary>
162-
public bool LogToConsole { get; set; } = false;
161+
protected bool WriteDriverLogToConsole { get; set; } = true;
163162

164163
/// <summary>
165164
/// Gets the command-line arguments for the driver service.
@@ -253,8 +252,8 @@ public void Start()
253252
this.driverServiceProcess.StartInfo.UseShellExecute = false;
254253
this.driverServiceProcess.StartInfo.CreateNoWindow = this.HideCommandPromptWindow;
255254

256-
this.driverServiceProcess.StartInfo.RedirectStandardOutput = this.LogToConsole;
257-
this.driverServiceProcess.StartInfo.RedirectStandardError = this.LogToConsole;
255+
this.driverServiceProcess.StartInfo.RedirectStandardOutput = this.WriteDriverLogToConsole;
256+
this.driverServiceProcess.StartInfo.RedirectStandardError = this.WriteDriverLogToConsole;
258257

259258
DriverProcessStartingEventArgs eventArgs = new DriverProcessStartingEventArgs(this.driverServiceProcess.StartInfo);
260259
this.OnDriverProcessStarting(eventArgs);
@@ -318,12 +317,12 @@ protected virtual void OnDriverProcessStarted(DriverProcessStartedEventArgs even
318317
throw new ArgumentNullException(nameof(eventArgs), "eventArgs must not be null");
319318
}
320319

321-
if (this.LogToConsole && eventArgs.StandardOutputStreamReader != null)
320+
if (this.WriteDriverLogToConsole && eventArgs.StandardOutputStreamReader != null)
322321
{
323322
_ = Task.Run(() => ReadStreamAsync(eventArgs.StandardOutputStreamReader, "stdout"));
324323
}
325324

326-
if (this.LogToConsole && eventArgs.StandardErrorStreamReader != null)
325+
if (this.WriteDriverLogToConsole && eventArgs.StandardErrorStreamReader != null)
327326
{
328327
_ = Task.Run(() => ReadStreamAsync(eventArgs.StandardErrorStreamReader, "stderr"));
329328
}

dotnet/src/webdriver/Firefox/FirefoxDriverService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ protected override void OnDriverProcessStarting(DriverProcessStartingEventArgs e
206206
{
207207
if (!string.IsNullOrEmpty(this.LogPath))
208208
{
209+
this.WriteDriverLogToConsole = false;
209210
string? directory = Path.GetDirectoryName(this.LogPath);
210211
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
211212
{

dotnet/test/firefox/FirefoxDriverServiceTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections.Generic;
2222
using NUnit.Framework;
2323
using System.IO;
24+
using System.Linq;
2425
using OpenQA.Selenium.Internal.Logging;
2526

2627
namespace OpenQA.Selenium.Firefox;
@@ -55,13 +56,14 @@ public void ShouldRedirectGeckoDriverLogsToFile()
5556
{
5657
FirefoxOptions options = new FirefoxOptions();
5758
string logPath = Path.GetTempFileName();
58-
options.LogLevel = FirefoxDriverLogLevel.Trace;
59+
options.LogLevel = FirefoxDriverLogLevel.Info;
5960

6061
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
6162
service.LogPath = logPath;
6263

6364
IWebDriver firefoxDriver = new FirefoxDriver(service, options);
64-
65+
firefoxDriver.Quit();
66+
6567
try
6668
{
6769
Assert.That(File.Exists(logPath), Is.True);
@@ -70,7 +72,6 @@ public void ShouldRedirectGeckoDriverLogsToFile()
7072
}
7173
finally
7274
{
73-
firefoxDriver.Quit();
7475
File.Delete(logPath);
7576
}
7677
}
@@ -83,14 +84,13 @@ public void ShouldRedirectGeckoDriverLogsToConsole()
8384
options.LogLevel = FirefoxDriverLogLevel.Info;
8485

8586
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
86-
service.LogToConsole = true;
8787

8888
IWebDriver firefoxDriver = new FirefoxDriver(service, options);
8989

9090
try
9191
{
9292
Assert.That(testLogHandler.Events, Has.Count.AtLeast(1));
93-
Assert.That(testLogHandler.Events[0].Message, Does.Contain("geckodriver"));
93+
Assert.That(testLogHandler.Events.Any(e => e.Message.Contains("geckodriver")), Is.True);
9494
}
9595
finally
9696
{

0 commit comments

Comments
 (0)