Skip to content

Commit 75b8eec

Browse files
authored
Make CLI Log Labels consistent with ASP.NET Core and use appropriate label on startup (#3307)
## Why make this change? Closes #3269 ## What is this change? Align the CLI logger labels with the labels used in ASP.Net Core. Downgraded internal plumbing message from `Information` to `Debug` Add a test case to cover the changed behavior. ## How was this tested? Added a test case to validate the new behavior.
1 parent b301eeb commit 75b8eec

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

src/Cli.Tests/CustomLoggerTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Cli.Tests;
5+
6+
/// <summary>
7+
/// Tests for CustomLoggerProvider and CustomConsoleLogger, verifying
8+
/// that log level labels use ASP.NET Core abbreviated format.
9+
/// </summary>
10+
[TestClass]
11+
public class CustomLoggerTests
12+
{
13+
/// <summary>
14+
/// Validates that each enabled log level produces the correct abbreviated label
15+
/// matching ASP.NET Core's default console formatter convention.
16+
/// Trace and Debug are below the logger's minimum level and produce no output.
17+
/// </summary>
18+
[DataTestMethod]
19+
[DataRow(LogLevel.Information, "info:")]
20+
[DataRow(LogLevel.Warning, "warn:")]
21+
[DataRow(LogLevel.Error, "fail:")]
22+
[DataRow(LogLevel.Critical, "crit:")]
23+
public void LogOutput_UsesAbbreviatedLogLevelLabels(LogLevel logLevel, string expectedPrefix)
24+
{
25+
CustomLoggerProvider provider = new();
26+
ILogger logger = provider.CreateLogger("TestCategory");
27+
28+
TextWriter originalOut = Console.Out;
29+
try
30+
{
31+
StringWriter writer = new();
32+
Console.SetOut(writer);
33+
34+
logger.Log(logLevel, "test message");
35+
36+
string output = writer.ToString();
37+
Assert.IsTrue(
38+
output.StartsWith(expectedPrefix),
39+
$"Expected output to start with '{expectedPrefix}' but got: '{output}'");
40+
Assert.IsTrue(
41+
output.Contains("test message"),
42+
$"Expected output to contain 'test message' but got: '{output}'");
43+
}
44+
finally
45+
{
46+
Console.SetOut(originalOut);
47+
}
48+
}
49+
}

src/Cli/CustomLoggerProvider.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ public class CustomConsoleLogger : ILogger
5656
{LogLevel.Critical, ConsoleColor.DarkRed}
5757
};
5858

59+
/// <summary>
60+
/// Maps LogLevel to abbreviated labels matching ASP.NET Core's default console formatter.
61+
/// </summary>
62+
private static readonly Dictionary<LogLevel, string> _logLevelToAbbreviation = new()
63+
{
64+
{LogLevel.Trace, "trce"},
65+
{LogLevel.Debug, "dbug"},
66+
{LogLevel.Information, "info"},
67+
{LogLevel.Warning, "warn"},
68+
{LogLevel.Error, "fail"},
69+
{LogLevel.Critical, "crit"}
70+
};
71+
5972
/// <summary>
6073
/// Creates Log message by setting console message color based on LogLevel.
6174
/// </summary>
@@ -66,11 +79,16 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
6679
return;
6780
}
6881

82+
if (!_logLevelToAbbreviation.TryGetValue(logLevel, out string? abbreviation))
83+
{
84+
return;
85+
}
86+
6987
ConsoleColor originalForeGroundColor = Console.ForegroundColor;
7088
ConsoleColor originalBackGroundColor = Console.BackgroundColor;
71-
Console.ForegroundColor = _logLevelToForeGroundConsoleColorMap[logLevel];
72-
Console.BackgroundColor = _logLevelToBackGroundConsoleColorMap[logLevel];
73-
Console.Write($"{logLevel}:");
89+
Console.ForegroundColor = _logLevelToForeGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.White);
90+
Console.BackgroundColor = _logLevelToBackGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.Black);
91+
Console.Write($"{abbreviation}:");
7492
Console.ForegroundColor = originalForeGroundColor;
7593
Console.BackgroundColor = originalBackGroundColor;
7694
Console.WriteLine($" {formatter(state, exception)}");

src/Service/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ private void ConfigureResponseCompression(IServiceCollection services, RuntimeCo
600600
options.Level = systemCompressionLevel;
601601
});
602602

603-
_logger.LogInformation("Response compression enabled with level '{compressionLevel}' for REST, GraphQL, and MCP endpoints.", compressionLevel);
603+
_logger.LogDebug("Response compression enabled with level '{compressionLevel}' for REST, GraphQL, and MCP endpoints.", compressionLevel);
604604
}
605605

606606
/// <summary>

0 commit comments

Comments
 (0)