Fixed logging (#626)
Build and Release / Read metadata (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Has been cancelled
Build and Release / Prepare & create release (push) Has been cancelled
Build and Release / Publish release (push) Has been cancelled

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-01-13 18:45:47 +01:00
1 parent 4f8266f255
commit eb39d130b9
9 files changed
+203 -45

No files matched your search

@@ -0,0 +1,10 @@
namespace AIStudio.Tools.Rust;
public readonly record struct LogEventRequest(
string Timestamp,
string Level,
string Category,
string Message,
string? Exception,
string? StackTrace
);
@@ -0,0 +1,3 @@
namespace AIStudio.Tools.Rust;
public readonly record struct LogEventResponse(bool Success, string Issue);
@@ -12,4 +12,28 @@ public sealed partial class RustService
{
return await this.http.GetFromJsonAsync<GetLogPathsResponse>("/log/paths", this.jsonRustSerializerOptions);
}
/// <summary>
/// Sends a log event to the Rust runtime.
/// </summary>
/// <param name="timestamp">The timestamp of the log event.</param>
/// <param name="level">The log level.</param>
/// <param name="category">The category of the log event.</param>
/// <param name="message">The log message.</param>
/// <param name="exception">Optional exception message.</param>
/// <param name="stackTrace">Optional exception stack trace.</param>
public void LogEvent(string timestamp, string level, string category, string message, string? exception = null, string? stackTrace = null)
{
try
{
// Fire-and-forget the log event to avoid blocking:
var request = new LogEventRequest(timestamp, level, category, message, exception, stackTrace);
_ = this.http.PostAsJsonAsync("/log/event", request, this.jsonRustSerializerOptions);
}
catch
{
Console.WriteLine("Failed to send log event to Rust service.");
// Ignore errors to avoid log loops
}
}
}
+75 -5
View File
@@ -1,3 +1,8 @@
using System.Collections.Concurrent;
using AIStudio.Tools.Rust;
using AIStudio.Tools.Services;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;
@@ -6,18 +11,83 @@ namespace AIStudio.Tools;
public sealed class TerminalLogger() : ConsoleFormatter(FORMATTER_NAME)
{
public const string FORMATTER_NAME = "AI Studio Terminal Logger";
private static RustService? RUST_SERVICE;
// Buffer for early log events before the RustService is available:
private static readonly ConcurrentQueue<LogEventRequest> EARLY_LOG_BUFFER = new();
// ANSI color codes for log levels:
private const string ANSI_RESET = "\x1b[0m";
private const string ANSI_GRAY = "\x1b[90m"; // Trace, Debug
private const string ANSI_GREEN = "\x1b[32m"; // Information
private const string ANSI_YELLOW = "\x1b[33m"; // Warning
private const string ANSI_RED = "\x1b[91m"; // Error, Critical
/// <summary>
/// Sets the Rust service for logging events and flushes any buffered early log events.
/// </summary>
/// <param name="service">The Rust service instance.</param>
public static void SetRustService(RustService service)
{
RUST_SERVICE = service;
// Flush all buffered early log events to Rust in the original order:
while (EARLY_LOG_BUFFER.TryDequeue(out var bufferedEvent))
{
service.LogEvent(
bufferedEvent.Timestamp,
bufferedEvent.Level,
bufferedEvent.Category,
bufferedEvent.Message,
bufferedEvent.Exception,
bufferedEvent.StackTrace
);
}
}
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
{
var message = logEntry.Formatter(logEntry.State, logEntry.Exception);
var timestamp = DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff");
var logLevel = logEntry.LogLevel.ToString();
var category = logEntry.Category;
textWriter.Write($"=> {timestamp} [{logLevel}] {category}: {message}");
var exceptionMessage = logEntry.Exception?.Message;
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)
textWriter.Write($" Exception was = {logEntry.Exception}");
{
textWriter.Write($" {colorCode}Exception: {exceptionMessage}{ANSI_RESET}");
if (stackTrace is not null)
{
textWriter.WriteLine();
foreach (var line in stackTrace.Split('\n'))
textWriter.WriteLine($" {line.TrimEnd()}");
}
}
else
textWriter.WriteLine();
// Send log event to Rust via API (fire-and-forget):
if (RUST_SERVICE is not null)
RUST_SERVICE.LogEvent(timestamp, logLevel, category, message, exceptionMessage, stackTrace);
textWriter.WriteLine();
// Buffer early log events until the RustService is available:
else
EARLY_LOG_BUFFER.Enqueue(new LogEventRequest(timestamp, logLevel, category, message, exceptionMessage, stackTrace));
}
private static string GetColorForLogLevel(LogLevel logLevel) => logLevel switch
{
LogLevel.Trace => ANSI_GRAY,
LogLevel.Debug => ANSI_GRAY,
LogLevel.Information => ANSI_GREEN,
LogLevel.Warning => ANSI_YELLOW,
LogLevel.Error => ANSI_RED,
LogLevel.Critical => ANSI_RED,
_ => ANSI_RESET
};
}