Added conditional stdout logging for TerminalLogger

This commit is contained in:
Thorsten Sommer 2026-02-25 07:48:00 +01:00
parent f79816232a
commit 145b8be2a7
Signed by untrusted user who does not match committer: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -13,6 +13,7 @@ public sealed class TerminalLogger() : ConsoleFormatter(FORMATTER_NAME)
public const string FORMATTER_NAME = "AI Studio Terminal Logger";
private static RustService? RUST_SERVICE;
private static bool LOG_TO_STDOUT = true;
// Buffer for early log events before the RustService is available:
private static readonly ConcurrentQueue<LogEventRequest> EARLY_LOG_BUFFER = new();
@ -44,6 +45,10 @@ public sealed class TerminalLogger() : ConsoleFormatter(FORMATTER_NAME)
bufferedEvent.StackTrace
);
}
#if !DEBUG
LOG_TO_STDOUT = false;
#endif
}
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
@ -56,19 +61,22 @@ public sealed class TerminalLogger() : ConsoleFormatter(FORMATTER_NAME)
var stackTrace = logEntry.Exception?.StackTrace;
var colorCode = GetColorForLogLevel(logEntry.LogLevel);
textWriter.Write($"[{colorCode}{timestamp}{ANSI_RESET}] {colorCode}{logLevel}{ANSI_RESET} [{category}] {colorCode}{message}{ANSI_RESET}");
if (logEntry.Exception is not null)
if (LOG_TO_STDOUT)
{
textWriter.Write($" {colorCode}Exception: {exceptionMessage}{ANSI_RESET}");
if (stackTrace is not null)
textWriter.Write($"[{colorCode}{timestamp}{ANSI_RESET}] {colorCode}{logLevel}{ANSI_RESET} [{category}] {colorCode}{message}{ANSI_RESET}");
if (logEntry.Exception is not null)
{
textWriter.WriteLine();
foreach (var line in stackTrace.Split('\n'))
textWriter.WriteLine($" {colorCode}{line.TrimEnd()}{ANSI_RESET}");
textWriter.Write($" {colorCode}Exception: {exceptionMessage}{ANSI_RESET}");
if (stackTrace is not null)
{
textWriter.WriteLine();
foreach (var line in stackTrace.Split('\n'))
textWriter.WriteLine($" {colorCode}{line.TrimEnd()}{ANSI_RESET}");
}
}
else
textWriter.WriteLine();
}
else
textWriter.WriteLine();
// Send log event to Rust via API (fire-and-forget):
if (RUST_SERVICE is not null)
@ -90,4 +98,4 @@ public sealed class TerminalLogger() : ConsoleFormatter(FORMATTER_NAME)
_ => ANSI_RESET
};
}
}