@@ -248,7 +248,15 @@ private async Task<ExitCode> ExecuteApp(
248248 skippedTestClasses : classMethodFilters ? . ToArray ( ) ,
249249 cancellationToken : cancellationToken ) ;
250250
251- return ParseResult ( testResult , resultMessage , appTester . ListenerConnected ) ;
251+ ExitCode exitCode = ParseResult ( testResult , resultMessage , appTester . ListenerConnected ) ;
252+
253+ if ( ! target . Platform . IsSimulator ( ) ) // Simulator app logs are already included in the main log
254+ {
255+ // Copy system and application logs to the main log for better failure investigation.
256+ CopyLogsToMainLog ( ) ;
257+ }
258+
259+ return exitCode ;
252260 }
253261
254262 private async Task < ExitCode > ExecuteMacCatalystApp (
@@ -278,7 +286,12 @@ private async Task<ExitCode> ExecuteMacCatalystApp(
278286 skippedTestClasses : classMethodFilters ? . ToArray ( ) ,
279287 cancellationToken : cancellationToken ) ;
280288
281- return ParseResult ( testResult , resultMessage , appTester . ListenerConnected ) ;
289+ ExitCode exitCode = ParseResult ( testResult , resultMessage , appTester . ListenerConnected ) ;
290+
291+ // Copy system and application logs to the main log for better failure investigation.
292+ CopyLogsToMainLog ( ) ;
293+
294+ return exitCode ;
282295 }
283296
284297 private IAppTester GetAppTester ( CommunicationChannel communicationChannel , bool isSimulator )
@@ -364,4 +377,38 @@ ExitCode LogProblem(string message, ExitCode defaultExitCode)
364377 return ExitCode . GENERAL_FAILURE ;
365378 }
366379 }
380+
381+ /// <summary>
382+ /// Copy system and application logs to the main log for better failure investigation.
383+ /// </summary>
384+ private void CopyLogsToMainLog ( )
385+ {
386+ var logs = _logs . Where ( log => log . Description == LogType . SystemLog . ToString ( ) || log . Description == LogType . ApplicationLog . ToString ( ) ) . ToList ( ) ;
387+
388+ foreach ( var log in logs )
389+ {
390+ _mainLog . WriteLine ( $ "==================== { log . Description } ====================") ;
391+ _mainLog . WriteLine ( $ "Log file: { log . FullPath } ") ;
392+
393+ try
394+ {
395+ // Read and append log content to the main log
396+ using var reader = log . GetReader ( ) ;
397+ while ( ! reader . EndOfStream )
398+ {
399+ var logContent = reader . ReadLine ( ) ;
400+ if ( logContent is null )
401+ continue ;
402+ _mainLog . WriteLine ( logContent ) ;
403+ }
404+ }
405+ catch ( Exception ex )
406+ {
407+ _mainLog . WriteLine ( $ "Failed to read { log . Description } : { ex . Message } ") ;
408+ }
409+
410+ _mainLog . WriteLine ( $ "==================== End of { log . Description } ====================") ;
411+ _mainLog . WriteLine ( string . Empty ) ;
412+ }
413+ }
367414}
0 commit comments