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