Skip to content

Commit db05817

Browse files
authored
[dotnet] Conditionally enable driver service process output redirection (#16353)
1 parent 1347c78 commit db05817

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

dotnet/src/webdriver/DriverService.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ public int ProcessId
170170
/// </summary>
171171
protected virtual bool HasShutdown => true;
172172

173+
/// <summary>
174+
/// Gets a value indicating whether process redirection is enforced regardless of other settings.
175+
/// </summary>
176+
/// <remarks>Set this property to <see langword="true"/> to force all process output and error streams to
177+
/// be redirected, even if redirection is not required by default behavior. This can be useful in scenarios where
178+
/// capturing process output is necessary for logging or analysis.</remarks>
179+
protected virtual internal bool EnableProcessRedirection { get; } = false;
180+
173181
/// <summary>
174182
/// Gets a value indicating whether the service is responding to HTTP requests.
175183
/// </summary>
@@ -249,16 +257,23 @@ public void Start()
249257
this.driverServiceProcess.StartInfo.RedirectStandardOutput = true;
250258
this.driverServiceProcess.StartInfo.RedirectStandardError = true;
251259

252-
this.driverServiceProcess.OutputDataReceived += this.OnDriverProcessDataReceived;
253-
this.driverServiceProcess.ErrorDataReceived += this.OnDriverProcessDataReceived;
260+
if (this.EnableProcessRedirection)
261+
{
262+
this.driverServiceProcess.OutputDataReceived += this.OnDriverProcessDataReceived;
263+
this.driverServiceProcess.ErrorDataReceived += this.OnDriverProcessDataReceived;
264+
}
254265

255266
DriverProcessStartingEventArgs eventArgs = new DriverProcessStartingEventArgs(this.driverServiceProcess.StartInfo);
256267
this.OnDriverProcessStarting(eventArgs);
257268

258-
// Important: Start the process and immediately begin reading the output and error streams to avoid IO deadlocks.
259269
this.driverServiceProcess.Start();
260-
this.driverServiceProcess.BeginOutputReadLine();
261-
this.driverServiceProcess.BeginErrorReadLine();
270+
271+
if (this.EnableProcessRedirection)
272+
{
273+
// Important: Start the process and immediately begin reading the output and error streams to avoid IO deadlocks.
274+
this.driverServiceProcess.BeginOutputReadLine();
275+
this.driverServiceProcess.BeginErrorReadLine();
276+
}
262277

263278
bool serviceAvailable = this.WaitForServiceInitialization();
264279

@@ -289,7 +304,7 @@ protected virtual void Dispose(bool disposing)
289304
{
290305
this.Stop();
291306

292-
if (this.driverServiceProcess is not null)
307+
if (EnableProcessRedirection && this.driverServiceProcess is not null)
293308
{
294309
this.driverServiceProcess.OutputDataReceived -= this.OnDriverProcessDataReceived;
295310
this.driverServiceProcess.ErrorDataReceived -= this.OnDriverProcessDataReceived;
@@ -335,13 +350,7 @@ protected virtual void OnDriverProcessStarted(DriverProcessStartedEventArgs even
335350
/// <param name="args">The data received event arguments.</param>
336351
protected virtual void OnDriverProcessDataReceived(object sender, DataReceivedEventArgs args)
337352
{
338-
if (string.IsNullOrEmpty(args.Data))
339-
return;
340353

341-
if (_logger.IsEnabled(LogEventLevel.Trace))
342-
{
343-
_logger.Trace(args.Data);
344-
}
345354
}
346355

347356
/// <summary>

dotnet/src/webdriver/Firefox/FirefoxDriverService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ protected override string CommandLineArguments
222222
}
223223
}
224224

225+
/// <summary>
226+
/// Gets a value indicating whether process output redirection is required.
227+
/// </summary>
228+
protected internal override bool EnableProcessRedirection => LogPath is not null;
229+
225230
/// <summary>
226231
/// Called when the driver process is starting. This method sets up log file writing if a log path is specified.
227232
/// </summary>

0 commit comments

Comments
 (0)