Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions dotnet/src/webdriver/DriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ public int ProcessId
/// </summary>
protected virtual bool HasShutdown => true;

/// <summary>
/// Gets a value indicating whether process redirection is enforced regardless of other settings.
/// </summary>
/// <remarks>Set this property to <see langword="true"/> to force all process output and error streams to
/// be redirected, even if redirection is not required by default behavior. This can be useful in scenarios where
/// capturing process output is necessary for logging or analysis.</remarks>
protected virtual internal bool EnableProcessRedirection { get; } = false;

/// <summary>
/// Gets a value indicating whether the service is responding to HTTP requests.
/// </summary>
Expand Down Expand Up @@ -249,16 +257,23 @@ public void Start()
this.driverServiceProcess.StartInfo.RedirectStandardOutput = true;
this.driverServiceProcess.StartInfo.RedirectStandardError = true;

this.driverServiceProcess.OutputDataReceived += this.OnDriverProcessDataReceived;
this.driverServiceProcess.ErrorDataReceived += this.OnDriverProcessDataReceived;
if (this.EnableProcessRedirection)
{
this.driverServiceProcess.OutputDataReceived += this.OnDriverProcessDataReceived;
this.driverServiceProcess.ErrorDataReceived += this.OnDriverProcessDataReceived;
}

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

// Important: Start the process and immediately begin reading the output and error streams to avoid IO deadlocks.
this.driverServiceProcess.Start();
this.driverServiceProcess.BeginOutputReadLine();
this.driverServiceProcess.BeginErrorReadLine();

if (this.EnableProcessRedirection)
{
// Important: Start the process and immediately begin reading the output and error streams to avoid IO deadlocks.
this.driverServiceProcess.BeginOutputReadLine();
this.driverServiceProcess.BeginErrorReadLine();
}

bool serviceAvailable = this.WaitForServiceInitialization();

Expand Down Expand Up @@ -289,7 +304,7 @@ protected virtual void Dispose(bool disposing)
{
this.Stop();

if (this.driverServiceProcess is not null)
if (EnableProcessRedirection && this.driverServiceProcess is not null)
{
this.driverServiceProcess.OutputDataReceived -= this.OnDriverProcessDataReceived;
this.driverServiceProcess.ErrorDataReceived -= this.OnDriverProcessDataReceived;
Expand Down Expand Up @@ -335,13 +350,7 @@ protected virtual void OnDriverProcessStarted(DriverProcessStartedEventArgs even
/// <param name="args">The data received event arguments.</param>
protected virtual void OnDriverProcessDataReceived(object sender, DataReceivedEventArgs args)
{
if (string.IsNullOrEmpty(args.Data))
return;

if (_logger.IsEnabled(LogEventLevel.Trace))
{
_logger.Trace(args.Data);
}
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions dotnet/src/webdriver/Firefox/FirefoxDriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ protected override string CommandLineArguments
}
}

/// <summary>
/// Gets a value indicating whether process output redirection is required.
/// </summary>
protected internal override bool EnableProcessRedirection => LogPath is not null;

/// <summary>
/// Called when the driver process is starting. This method sets up log file writing if a log path is specified.
/// </summary>
Expand Down
Loading