1919
2020using OpenQA . Selenium . Internal ;
2121using System ;
22+ using System . Diagnostics ;
2223using System . Globalization ;
2324using System . IO ;
2425using System . Text ;
25- using System . Threading . Tasks ;
2626
2727namespace OpenQA . Selenium . Firefox ;
2828
@@ -223,11 +223,11 @@ protected override string CommandLineArguments
223223 }
224224
225225 /// <summary>
226- /// Handles the event when the driver service process is starting.
226+ /// Called when the driver process is starting. This method sets up log file writing if a log path is specified .
227227 /// </summary>
228228 /// <param name="eventArgs">The event arguments containing information about the driver service process.</param>
229229 /// <remarks>
230- /// This method initializes a log writer if a log path is specified and redirects output streams to capture logs .
230+ /// This method initializes a log writer if a log path is specified.
231231 /// </remarks>
232232 protected override void OnDriverProcessStarting ( DriverProcessStartingEventArgs eventArgs )
233233 {
@@ -239,40 +239,36 @@ protected override void OnDriverProcessStarting(DriverProcessStartingEventArgs e
239239 Directory . CreateDirectory ( directory ) ;
240240 }
241241
242- // Initialize the log writer
243242 logWriter = new StreamWriter ( this . LogPath , append : true ) { AutoFlush = true } ;
244-
245- // Configure process to redirect output
246- eventArgs . DriverServiceProcessStartInfo . RedirectStandardOutput = true ;
247- eventArgs . DriverServiceProcessStartInfo . RedirectStandardError = true ;
248243 }
249244
250245 base . OnDriverProcessStarting ( eventArgs ) ;
251246 }
252247
253248 /// <summary>
254- /// Handles the event when the driver process has started .
249+ /// Handles the output and error data received from the driver process and sends it to the log writer if available .
255250 /// </summary>
256- /// <param name="eventArgs">The event arguments containing information about the started driver process.</param>
257- /// <remarks>
258- /// This method reads the output and error streams asynchronously and writes them to the log file if available.
259- /// </remarks>
260- protected override void OnDriverProcessStarted ( DriverProcessStartedEventArgs eventArgs )
251+ /// <param name="sender">The sender of the event.</param>
252+ /// <param name="args">The data received event arguments.</param>
253+ protected override void OnDriverProcessDataReceived ( object sender , DataReceivedEventArgs args )
261254 {
262- if ( logWriter == null ) return ;
263- if ( eventArgs . StandardOutputStreamReader != null )
255+ if ( string . IsNullOrEmpty ( args . Data ) )
256+ return ;
257+
258+ if ( ! string . IsNullOrEmpty ( this . LogPath ) )
264259 {
265- _ = Task . Run ( ( ) => ReadStreamAsync ( eventArgs . StandardOutputStreamReader ) ) ;
260+ if ( logWriter != null )
261+ {
262+ logWriter . WriteLine ( args . Data ) ;
263+ }
266264 }
267-
268- if ( eventArgs . StandardErrorStreamReader != null )
265+ else
269266 {
270- _ = Task . Run ( ( ) => ReadStreamAsync ( eventArgs . StandardErrorStreamReader ) ) ;
267+ base . OnDriverProcessDataReceived ( sender , args ) ;
271268 }
272-
273- base . OnDriverProcessStarted ( eventArgs ) ;
274269 }
275270
271+
276272 /// <summary>
277273 /// Disposes of the resources used by the <see cref="FirefoxDriverService"/> instance.
278274 /// </summary>
@@ -282,13 +278,13 @@ protected override void OnDriverProcessStarted(DriverProcessStartedEventArgs eve
282278 /// </remarks>
283279 protected override void Dispose ( bool disposing )
284280 {
281+ base . Dispose ( disposing ) ;
282+
285283 if ( logWriter != null && disposing )
286284 {
287285 logWriter . Dispose ( ) ;
288286 logWriter = null ;
289287 }
290-
291- base . Dispose ( disposing ) ;
292288 }
293289
294290 /// <summary>
@@ -372,24 +368,4 @@ private static string FirefoxDriverServiceFileName()
372368
373369 return fileName ;
374370 }
375-
376- private async Task ReadStreamAsync ( StreamReader reader )
377- {
378- try
379- {
380- string ? line ;
381- while ( ( line = await reader . ReadLineAsync ( ) ) != null )
382- {
383- if ( logWriter != null )
384- {
385- logWriter . WriteLine ( $ "{ DateTime . Now : yyyy-MM-dd HH:mm:ss.fff} { line } ") ;
386- }
387- }
388- }
389- catch ( Exception ex )
390- {
391- // Log or handle the exception appropriately
392- System . Diagnostics . Debug . WriteLine ( $ "Error reading stream: { ex . Message } ") ;
393- }
394- }
395371}
0 commit comments