Skip to content

Commit 7b6f582

Browse files
authored
[release/9.0] Backport mobile log unification (#1453)
Combined backport of #1348 and #1383 to ensure we're able to track mobile test failures on release/9.0 branch as well.
1 parent 9c15ebd commit 7b6f582

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/Microsoft.DotNet.XHarness.Android/AdbRunner.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ public bool TryDumpAdbLog(string outputFilePath, string filterSpec = "")
154154
Directory.CreateDirectory(Path.GetDirectoryName(outputFilePath) ?? throw new ArgumentNullException(nameof(outputFilePath)));
155155
File.WriteAllText(outputFilePath, result.StandardOutput);
156156
_log.LogInformation($"Wrote current ADB log to {outputFilePath}");
157+
// The adb log is not directly accessible.
158+
// Hence, we duplicate the log to the main console log to simplify the UX of failure investigation.
159+
_log.LogInformation($"ADB log output:{Environment.NewLine}{result.StandardOutput}");
157160
return true;
158161
}
159162
}

src/Microsoft.DotNet.XHarness.Apple/AppOperations/AppTester.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ public AppTester(
190190
using var crashLogs = new Logs(_logs.Directory);
191191

192192
ICrashSnapshotReporter crashReporter = _snapshotReporterFactory.Create(_mainLog, crashLogs, isDevice: !isSimulator, device.Name);
193-
using ITestReporter testReporter = _testReporterFactory.Create(_mainLog,
193+
using ITestReporter testReporter = _testReporterFactory.Create(
194+
_mainLog,
194195
_mainLog,
195196
_logs,
196197
crashReporter,
@@ -361,6 +362,7 @@ private async Task RunDeviceTests(
361362
// We need to check for MT1111 (which means that mlaunch won't wait for the app to exit)
362363
IFileBackedLog aggregatedLog = Log.CreateReadableAggregatedLog(_mainLog, testReporter.CallbackLog);
363364

365+
364366
var result = await RunAndWatchForAppSignal(() => _processManager.ExecuteCommandAsync(
365367
mlaunchArguments,
366368
aggregatedLog,

src/Microsoft.DotNet.XHarness.Apple/Orchestration/TestOrchestrator.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)