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