AI-Studio/app/MindWork AI Studio/Tools/TerminalLogger.cs

40 lines
1.4 KiB
C#
Raw Normal View History

2026-01-13 09:00:36 +00:00
using AIStudio.Tools.Services;
2024-09-01 18:10:03 +00:00
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;
namespace AIStudio.Tools;
public sealed class TerminalLogger() : ConsoleFormatter(FORMATTER_NAME)
{
public const string FORMATTER_NAME = "AI Studio Terminal Logger";
2026-01-13 09:00:36 +00:00
private static RustService? RUST_SERVICE;
/// <summary>
/// Sets the Rust service for logging events.
/// </summary>
/// <param name="service">The Rust service instance.</param>
public static void SetRustService(RustService service)
{
RUST_SERVICE = service;
}
2024-09-01 18:10:03 +00:00
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;
2026-01-13 09:00:36 +00:00
var exception = logEntry.Exception?.ToString();
textWriter.Write($"[{timestamp}] {logLevel} [{category}] {message}");
if (exception is not null)
textWriter.Write($" Exception: {exception}");
2024-09-01 18:10:03 +00:00
textWriter.WriteLine();
2026-01-13 09:00:36 +00:00
// Send log event to Rust via API (fire-and-forget):
RUST_SERVICE?.LogEvent(timestamp, logLevel, category, message, exception);
2024-09-01 18:10:03 +00:00
}
}