Cleanup cake process logging (#1293)

This commit is contained in:
Charles Gagnon
2021-11-10 16:44:54 -08:00
committed by GitHub
parent c81a400752
commit c738e3bdf7
2 changed files with 23 additions and 24 deletions

View File

@@ -220,14 +220,17 @@ Task("BuildTest")
{ {
var project = pair.Key; var project = pair.Key;
var projectFolder = System.IO.Path.Combine(testFolder, project); var projectFolder = System.IO.Path.Combine(testFolder, project);
var runLog = new List<string>(); var logPath = System.IO.Path.Combine(logFolder, $"{project}-{framework}-build.log");
using (var logWriter = new StreamWriter(logPath)) {
Run(dotnetcli, $"build --framework {framework} --configuration {testConfiguration} \"{projectFolder}\"", Run(dotnetcli, $"build --framework {framework} --configuration {testConfiguration} \"{projectFolder}\"",
new RunOptions new RunOptions
{ {
StandardOutputListing = runLog StandardOutputWriter = logWriter,
StandardErrorWriter = logWriter
}) })
.ExceptionOnError($"Building test {project} failed for {framework}."); .ExceptionOnError($"Building test {project} failed for {framework}.");
System.IO.File.WriteAllLines(System.IO.Path.Combine(logFolder, $"{project}-{framework}-build.log"), runLog.ToArray()); }
} }
} }
}); });

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
/// <summary> /// <summary>
/// Class encompassing the optional settings for running processes. /// Class encompassing the optional settings for running processes.
@@ -11,13 +12,13 @@ public class RunOptions
/// </summary> /// </summary>
public string WorkingDirectory { get; set; } public string WorkingDirectory { get; set; }
/// <summary> /// <summary>
/// Container logging the StandardOutput content. /// Stream to log std out messages to
/// </summary> /// </summary>
public IList<string> StandardOutputListing { get; set; } public StreamWriter StandardOutputWriter { get; set; }
/// <summary> /// <summary>
/// Container logging the Error content. /// Stream to log std err messages to
/// </summary> /// </summary>
public IList<string> StandardErrorListing { get; set; } public StreamWriter StandardErrorWriter { get; set; }
/// <summary> /// <summary>
/// Desired maximum time-out for the process /// Desired maximum time-out for the process
/// </summary> /// </summary>
@@ -121,27 +122,27 @@ ExitStatus Run(string exec, string args, RunOptions runOptions)
{ {
WorkingDirectory = workingDirectory, WorkingDirectory = workingDirectory,
UseShellExecute = false, UseShellExecute = false,
RedirectStandardOutput = runOptions.StandardOutputListing != null, RedirectStandardOutput = runOptions.StandardOutputWriter != null,
RedirectStandardError = runOptions.StandardErrorListing != null, RedirectStandardError = runOptions.StandardErrorWriter != null,
}); });
if (runOptions.StandardOutputListing != null) if (runOptions.StandardOutputWriter != null)
{ {
process.OutputDataReceived += (s, e) => process.OutputDataReceived += (s, e) =>
{ {
if (e.Data != null) if (e.Data != null)
{ {
runOptions.StandardOutputListing.Add(e.Data); runOptions.StandardOutputWriter.WriteLine(e.Data);
} }
}; };
process.BeginOutputReadLine(); process.BeginOutputReadLine();
} }
if (runOptions.StandardErrorListing != null) if (runOptions.StandardErrorWriter != null)
{ {
process.ErrorDataReceived += (s, e) => process.ErrorDataReceived += (s, e) =>
{ {
if (e.Data != null) if (e.Data != null)
{ {
runOptions.StandardErrorListing.Add(e.Data); runOptions.StandardErrorWriter.WriteLine(e.Data);
} }
}; };
process.BeginErrorReadLine(); process.BeginErrorReadLine();
@@ -220,21 +221,16 @@ public void KillProcessTree(Process process)
} }
public void DotnetPack(string outputFolder, string projectFolder, string project) { public void DotnetPack(string outputFolder, string projectFolder, string project) {
var runLog = new List<string>();
var errorLog = new List<string>();
var logPath = System.IO.Path.Combine(logFolder, $"{project}-pack.log"); var logPath = System.IO.Path.Combine(logFolder, $"{project}-pack.log");
using (var logWriter = new StreamWriter(logPath)) {
Information($"Packaging {projectFolder}"); Information($"Packaging {projectFolder}");
try {
Run(dotnetcli, $"pack --configuration {configuration} --output {outputFolder} \"{projectFolder}\"", Run(dotnetcli, $"pack --configuration {configuration} --output {outputFolder} \"{projectFolder}\"",
new RunOptions new RunOptions
{ {
StandardOutputListing = runLog, StandardOutputWriter = logWriter,
StandardErrorListing = errorLog StandardErrorWriter = logWriter
}) })
.ExceptionOnError($"Packaging {project} failed. See {logPath} for details."); .ExceptionOnError($"Packaging {project} failed. See {logPath} for details.");
} catch(Exception) {
System.IO.File.WriteAllLines(logPath, runLog.ToArray());
throw;
} }
System.IO.File.WriteAllLines(logPath, runLog.ToArray());
} }