2525using System . IO ;
2626using System . Net ;
2727using System . Net . Http ;
28- using System . Threading ;
2928using System . Threading . Tasks ;
3029using OpenQA . Selenium . Internal . Logging ;
3130
@@ -253,8 +252,11 @@ public void Start()
253252 this . driverServiceProcess . StartInfo . UseShellExecute = false ;
254253 this . driverServiceProcess . StartInfo . CreateNoWindow = this . HideCommandPromptWindow ;
255254
256- this . driverServiceProcess . StartInfo . RedirectStandardOutput = this . WriteDriverLogToConsole ;
257- this . driverServiceProcess . StartInfo . RedirectStandardError = this . WriteDriverLogToConsole ;
255+ this . driverServiceProcess . StartInfo . RedirectStandardOutput = true ;
256+ this . driverServiceProcess . StartInfo . RedirectStandardError = true ;
257+
258+ this . driverServiceProcess . OutputDataReceived += ( s , e ) => this . OnDriverProcessDataReceived ( s , e , isError : false ) ;
259+ this . driverServiceProcess . ErrorDataReceived += ( s , e ) => this . OnDriverProcessDataReceived ( s , e , isError : true ) ;
258260
259261 DriverProcessStartingEventArgs eventArgs = new DriverProcessStartingEventArgs ( this . driverServiceProcess . StartInfo ) ;
260262 this . OnDriverProcessStarting ( eventArgs ) ;
@@ -264,6 +266,9 @@ public void Start()
264266 DriverProcessStartedEventArgs processStartedEventArgs = new DriverProcessStartedEventArgs ( this . driverServiceProcess ) ;
265267 this . OnDriverProcessStarted ( processStartedEventArgs ) ;
266268
269+ this . driverServiceProcess . BeginOutputReadLine ( ) ;
270+ this . driverServiceProcess . BeginErrorReadLine ( ) ;
271+
267272 if ( ! serviceAvailable )
268273 {
269274 throw new WebDriverException ( $ "Cannot start the driver service on { this . ServiceUrl } ") ;
@@ -318,27 +323,28 @@ protected virtual void OnDriverProcessStarted(DriverProcessStartedEventArgs even
318323 throw new ArgumentNullException ( nameof ( eventArgs ) , "eventArgs must not be null" ) ;
319324 }
320325
321- if ( this . WriteDriverLogToConsole && eventArgs . StandardOutputStreamReader != null )
326+ this . DriverProcessStarted ? . Invoke ( this , eventArgs ) ;
327+ }
328+
329+ /// <summary>
330+ /// Handles the output and error data received from the driver process.
331+ /// </summary>
332+ /// <param name="sender">The sender of the event.</param>
333+ /// <param name="args">The data received event arguments.</param>
334+ /// <param name="isError">A value indicating whether the data received is from the error stream.</param>
335+ protected virtual void OnDriverProcessDataReceived ( object sender , DataReceivedEventArgs args , bool isError )
336+ {
337+ if ( string . IsNullOrEmpty ( args . Data ) )
338+ return ;
339+
340+ if ( this . WriteDriverLogToConsole && ! isError && _logger . IsEnabled ( LogEventLevel . Info ) )
322341 {
323- var stdoutThread = new Thread ( ( ) => ReadStreamSync ( eventArgs . StandardOutputStreamReader , "stdout" ) )
324- {
325- IsBackground = true ,
326- Name = "DriverService-stdout"
327- } ;
328- stdoutThread . Start ( ) ;
342+ _logger . Info ( args . Data ) ;
329343 }
330-
331- if ( this . WriteDriverLogToConsole && eventArgs . StandardErrorStreamReader != null )
344+ if ( this . WriteDriverLogToConsole && isError && _logger . IsEnabled ( LogEventLevel . Error ) )
332345 {
333- var stderrThread = new Thread ( ( ) => ReadStreamSync ( eventArgs . StandardErrorStreamReader , "stderr" ) )
334- {
335- IsBackground = true ,
336- Name = "DriverService-stderr"
337- } ;
338- stderrThread . Start ( ) ;
346+ _logger . Error ( args . Data ) ;
339347 }
340-
341- this . DriverProcessStarted ? . Invoke ( this , eventArgs ) ;
342348 }
343349
344350 /// <summary>
@@ -419,69 +425,4 @@ private bool WaitForServiceInitialization()
419425
420426 return isInitialized ;
421427 }
422-
423- private void ReadStreamSync ( StreamReader reader , string streamType )
424- {
425- try
426- {
427- string ? line ;
428- while ( ( line = reader . ReadLine ( ) ) != null )
429- {
430- if ( streamType . Equals ( "stdout" , StringComparison . OrdinalIgnoreCase ) )
431- {
432- if ( _logger . IsEnabled ( LogEventLevel . Info ) )
433- {
434- _logger . Info ( line ) ;
435- }
436- }
437- else
438- {
439- if ( _logger . IsEnabled ( LogEventLevel . Error ) )
440- {
441- _logger . Error ( line ) ;
442- }
443- }
444- }
445- }
446- catch ( Exception ex )
447- {
448- if ( _logger . IsEnabled ( LogEventLevel . Error ) )
449- {
450- _logger . Error ( $ "Error reading stream: { ex } ") ;
451- }
452- }
453- }
454-
455- private async Task ReadStreamAsync ( StreamReader reader , string streamType )
456- {
457- try
458- {
459- string ? line ;
460- while ( ( line = await reader . ReadLineAsync ( ) ) != null )
461- {
462- if ( streamType . Equals ( "stdout" , StringComparison . OrdinalIgnoreCase ) )
463- {
464- if ( _logger . IsEnabled ( LogEventLevel . Info ) )
465- {
466- _logger . Info ( line ) ;
467- }
468- }
469- else
470- {
471- if ( _logger . IsEnabled ( LogEventLevel . Error ) )
472- {
473- _logger . Error ( line ) ;
474- }
475- }
476- }
477- }
478- catch ( Exception ex )
479- {
480- if ( _logger . IsEnabled ( LogEventLevel . Error ) )
481- {
482- _logger . Error ( $ "Error reading stream: { ex } ") ;
483- }
484- }
485- }
486-
487428}
0 commit comments