Skip to content

Commit 566bf11

Browse files
authored
feat: add UTC timestamps to console log entries
1 parent 1bc2a58 commit 566bf11

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/Cli.Tests/CustomLoggerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public void LogOutput_UsesAbbreviatedLogLevelLabels(LogLevel logLevel, string ex
7777
string actual = expectStderr ? stderr : stdout;
7878
string other = expectStderr ? stdout : stderr;
7979

80-
Assert.IsTrue(actual.StartsWith(expectedPrefix),
81-
$"Expected output to start with '{expectedPrefix}' but got: '{actual}'");
80+
Assert.IsTrue(actual.Contains(expectedPrefix),
81+
$"Expected output to contain '{expectedPrefix}' but got: '{actual}'");
8282
StringAssert.Contains(actual, Message);
8383
Assert.AreEqual(string.Empty, other,
8484
$"Did not expect output on the other stream but got: '{other}'");

src/Cli/CustomLoggerProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
124124
// Apply colors so the abbreviation matches the visual style of engine logs.
125125
// try/finally guarantees the original colors are restored even if Write throws,
126126
// 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'");
127128
ConsoleColor mcpOriginalForeGroundColor = Console.ForegroundColor;
128129
ConsoleColor mcpOriginalBackGroundColor = Console.BackgroundColor;
129130
try
130131
{
131132
Console.ForegroundColor = _logLevelToForeGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.White);
132133
Console.BackgroundColor = _logLevelToBackGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.Black);
133-
Console.Error.Write($"{mcpAbbreviation}:");
134+
Console.Error.Write($"{mcpTimestamp} {mcpAbbreviation}:");
134135
}
135136
finally
136137
{
@@ -153,6 +154,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
153154
}
154155

155156
TextWriter writer = logLevel >= LogLevel.Error ? Console.Error : Console.Out;
157+
string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'");
156158
// try/finally guarantees the original colors are restored even if Write throws,
157159
// otherwise the console would be left tinted (e.g. red on error) for subsequent output.
158160
ConsoleColor originalForeGroundColor = Console.ForegroundColor;
@@ -161,7 +163,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
161163
{
162164
Console.ForegroundColor = _logLevelToForeGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.White);
163165
Console.BackgroundColor = _logLevelToBackGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.Black);
164-
writer.Write($"{abbreviation}:");
166+
writer.Write($"{timestamp} {abbreviation}:");
165167
}
166168
finally
167169
{

src/Service/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ public static IHostBuilder CreateHostBuilder(string[] args, bool runMcpStdio, st
194194
else
195195
{
196196
logging.SetMinimumLevel(LogLevelProvider.CurrentLogLevel);
197+
logging.AddSimpleConsole(options =>
198+
{
199+
options.TimestampFormat = "yyyy-MM-dd'T'HH:mm:ss.fff'Z' ";
200+
options.UseUtcTimestamp = true;
201+
});
197202
}
198203

199204
// Add filter for dynamic log level changes (e.g., via MCP logging/setLevel)
@@ -464,6 +469,11 @@ public static ILoggerFactory GetLoggerFactoryForLogLevel(
464469
// When LogLevel.None, skip the console logger entirely for true silence.
465470
if (LogLevelProvider.CurrentLogLevel != LogLevel.None)
466471
{
472+
builder.AddSimpleConsole(options =>
473+
{
474+
options.TimestampFormat = "yyyy-MM-dd'T'HH:mm:ss.fff'Z' ";
475+
options.UseUtcTimestamp = true;
476+
});
467477
builder.AddConsole(options =>
468478
{
469479
options.LogToStandardErrorThreshold = LogLevel.Trace;
@@ -472,7 +482,11 @@ public static ILoggerFactory GetLoggerFactoryForLogLevel(
472482
}
473483
else
474484
{
475-
builder.AddConsole();
485+
builder.AddSimpleConsole(options =>
486+
{
487+
options.TimestampFormat = "yyyy-MM-dd'T'HH:mm:ss.fff'Z' ";
488+
options.UseUtcTimestamp = true;
489+
});
476490
}
477491
});
478492
}

0 commit comments

Comments
 (0)