Skip to content

Commit 314e0dd

Browse files
committed
chore: fix exception logging on stdout redirected environment
1 parent ea5db04 commit 314e0dd

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Spectre.Console;
5+
using Spectre.Console.Cli;
6+
7+
namespace Docfx;
8+
9+
internal static class SpectreConsoleExtensions
10+
{
11+
public static void WriteException(this IAnsiConsole ansiConsole, Exception e)
12+
{
13+
if (e is CommandAppException cae)
14+
{
15+
if (cae.Pretty is { } pretty)
16+
AnsiConsole.Write(pretty);
17+
else
18+
AnsiConsole.MarkupInterpolated($"[red]Error:[/] {e.Message}");
19+
return;
20+
}
21+
else
22+
{
23+
AnsiConsole.WriteException(e, new ExceptionSettings()
24+
{
25+
Format = ExceptionFormats.ShortenEverything,
26+
Style = new()
27+
{
28+
ParameterName = Color.Grey,
29+
ParameterType = Color.Grey78,
30+
LineNumber = Color.Grey78,
31+
},
32+
});
33+
}
34+
}
35+
}

src/docfx/Program.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Docfx.Common;
45
using Spectre.Console;
56
using Spectre.Console.Cli;
67

@@ -39,26 +40,34 @@ internal static int Main(string[] args)
3940

4041
static void OnException(Exception e, ITypeResolver? resolver)
4142
{
42-
if (e is CommandAppException cae)
43+
// Try to unwrap AggregateException.
44+
if (e is AggregateException ae && ae.InnerExceptions.Count == 1)
45+
e = ae.InnerExceptions[0];
46+
47+
if (!Console.IsOutputRedirected)
4348
{
44-
if (cae.Pretty is { } pretty)
45-
AnsiConsole.Write(pretty);
46-
else
47-
AnsiConsole.MarkupInterpolated($"[red]Error:[/] {e.Message}");
49+
// Write exception to console.
50+
AnsiConsole.Console.WriteException(e);
51+
52+
// Write exception to ReportLogListener if exists.
53+
var reportLogListener = Logger.FindListener(x => x is ReportLogListener);
54+
reportLogListener?.WriteLine(Logger.GetLogItem(LogLevel.Error, e.ToString(), code: ErrorCodes.Build.FatalError));
4855
}
4956
else
5057
{
51-
AnsiConsole.WriteException(e, new ExceptionSettings()
52-
{
53-
Format = ExceptionFormats.ShortenEverything,
54-
Style = new()
55-
{
56-
ParameterName = Color.Grey,
57-
ParameterType = Color.Grey78,
58-
LineNumber = Color.Grey78,
59-
},
60-
});
58+
// Write exception with Logger API if stdout is redirected.
59+
// To avoid line wrap issue https://github.com/spectreconsole/spectre.console/issues/1782
60+
var exceptions = e is AggregateException ae2
61+
? ae2.Flatten().InnerExceptions.ToArray()
62+
: [e];
63+
64+
foreach (var ex in exceptions)
65+
Logger.LogError(e.ToString(), code: ErrorCodes.Build.FatalError);
6166
}
67+
68+
// Cleanup logger.
69+
Logger.Flush();
70+
Logger.UnregisterAllListeners();
6271
}
6372
}
6473
}

0 commit comments

Comments
 (0)