Skip to content

Commit 1808c9d

Browse files
filzrevyufeih
andauthored
fix: Modify exception logging behaviors (#10950)
* chore: fix exception logging on stdout redirected environment * chore: fix failed test on CI by resetting error count --------- Co-authored-by: Yufei Huang <yufeih@live.com>
1 parent 4b34179 commit 1808c9d

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
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
}

test/docfx.Tests/CommandLineTest.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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;
5+
46
namespace Docfx.Tests;
57

68
[Collection("docfx STA")]
7-
public static class CommandLineTest
9+
public class CommandLineTest
810
{
911
[Fact]
1012
public static void PrintsVersion()
@@ -31,7 +33,14 @@ public static void PrintsHelp()
3133
[Fact]
3234
public static void FailForUnknownArgs()
3335
{
34-
Assert.Equal(-1, Program.Main(["--unknown"]));
36+
try
37+
{
38+
Assert.Equal(-1, Program.Main(["--unknown"]));
39+
}
40+
finally
41+
{
42+
Logger.ResetCount();
43+
}
3544
}
3645

3746
[Fact]

0 commit comments

Comments
 (0)