Skip to content

Commit

Permalink
chore(compilation): Improve Error and Trace Logging (#2688)
Browse files Browse the repository at this point in the history
Add logs

Co-authored-by: Rouke Broersma <rouke.broersma@infosupport.com>
  • Loading branch information
JackSteel97 and rouke-broersma authored Sep 29, 2023
1 parent 4696956 commit 579f32a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Stryker.Core/Stryker.Core/Compiling/CsharpCompilingProcess.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -74,6 +75,7 @@ public CompilingProcessResult Compile(IEnumerable<SyntaxTree> syntaxTrees, Strea
{
_logger.LogError("Failed to build the mutated assembly due to unrecoverable error: {0}",
emitResult.Diagnostics.First(diagnostic => diagnostic.Location == Location.None && diagnostic.Severity == DiagnosticSeverity.Error));
DumpErrorDetails(emitResult.Diagnostics);
throw new CompilationException("General Build Failure detected.");
}

Expand Down Expand Up @@ -154,7 +156,6 @@ private CSharpCompilation RunSourceGenerators(IAnalyzerResult analyzerResult, Co
null,
null),
options: emitOptions);

LogEmitResult(emitResult);

return (rollbackProcessResult, emitResult, retryCount+1);
Expand All @@ -168,7 +169,7 @@ private void LogEmitResult(EmitResult result)

foreach (var err in result.Diagnostics.Where(x => x.Severity is DiagnosticSeverity.Error))
{
_logger.LogDebug("{0}, {1}", err?.GetMessage() ?? "No message", err?.Location.SourceTree?.FilePath ?? "Unknown filepath");
_logger.LogDebug("{ErrorMessage}, {ErrorLocation}", err?.GetMessage() ?? "No message", err?.Location?.ToString() ?? "Unknown filepath");
}
}
else
Expand All @@ -177,6 +178,23 @@ private void LogEmitResult(EmitResult result)
}
}

private void DumpErrorDetails(IEnumerable<Diagnostic> diagnostics)
{
var message = $"An unrecoverable compilation error occurred:{Environment.NewLine}";
var materializedDiagnostics = diagnostics.ToArray();
if (!materializedDiagnostics.Any())
{
message += "Unfortunately there is no more info available, good luck!";
}

foreach (var diagnostic in materializedDiagnostics)
{
message += $"{diagnostic.Id}: {diagnostic}{Environment.NewLine}";
}

_logger.LogTrace(message);
}

private static string ReadableNumber(int number) => number switch
{
1 => "first",
Expand Down

0 comments on commit 579f32a

Please sign in to comment.