Skip to content

Commit 405f145

Browse files
authored
refactor: extract timestamp constant and clarify stdio console config
1 parent 566bf11 commit 405f145

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

src/Cli/CustomLoggerProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public ILogger CreateLogger(string categoryName)
2525

2626
public class CustomConsoleLogger : ILogger
2727
{
28+
private const string UtcTimestampFormat = "yyyy-MM-dd'T'HH:mm:ss.fff'Z'";
29+
2830
private readonly LogLevel _minimumLogLevel;
2931

3032
// Minimum LogLevel for CLI output.
@@ -124,7 +126,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
124126
// Apply colors so the abbreviation matches the visual style of engine logs.
125127
// try/finally guarantees the original colors are restored even if Write throws,
126128
// otherwise the console would be left tinted (e.g. red on error) for subsequent output.
127-
string mcpTimestamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
129+
string mcpTimestamp = DateTime.UtcNow.ToString(UtcTimestampFormat);
128130
ConsoleColor mcpOriginalForeGroundColor = Console.ForegroundColor;
129131
ConsoleColor mcpOriginalBackGroundColor = Console.BackgroundColor;
130132
try
@@ -154,7 +156,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
154156
}
155157

156158
TextWriter writer = logLevel >= LogLevel.Error ? Console.Error : Console.Out;
157-
string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
159+
string timestamp = DateTime.UtcNow.ToString(UtcTimestampFormat);
158160
// try/finally guarantees the original colors are restored even if Write throws,
159161
// otherwise the console would be left tinted (e.g. red on error) for subsequent output.
160162
ConsoleColor originalForeGroundColor = Console.ForegroundColor;

src/Service/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Microsoft.Extensions.Hosting;
2727
using Microsoft.Extensions.Logging;
2828
using Microsoft.Extensions.Logging.ApplicationInsights;
29+
using Microsoft.Extensions.Logging.Console;
2930
using OpenTelemetry.Exporter;
3031
using OpenTelemetry.Logs;
3132
using OpenTelemetry.Resources;
@@ -474,7 +475,9 @@ public static ILoggerFactory GetLoggerFactoryForLogLevel(
474475
options.TimestampFormat = "yyyy-MM-dd'T'HH:mm:ss.fff'Z' ";
475476
options.UseUtcTimestamp = true;
476477
});
477-
builder.AddConsole(options =>
478+
// Route all levels to stderr to keep stdout clean for MCP JSON-RPC.
479+
// Uses Services.Configure (not AddConsole) so no second provider is registered.
480+
builder.Services.Configure<ConsoleLoggerOptions>(options =>
478481
{
479482
options.LogToStandardErrorThreshold = LogLevel.Trace;
480483
});

0 commit comments

Comments
 (0)