turing-machine/Implementation/Server/MinimalConsoleFormatter.cs

27 lines
878 B
C#

using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;
namespace TuringMachine.Server;
/// <summary>
/// Custom log formatter that outputs a minimal timestamp + level + message format.
/// </summary>
public sealed class MinimalConsoleFormatter : ConsoleFormatter
{
public MinimalConsoleFormatter() : base("minimal") { }
public override void Write<TState>(
in LogEntry<TState> logEntry,
IExternalScopeProvider? scopeProvider,
TextWriter textWriter)
{
string? message = logEntry.Formatter?.Invoke(logEntry.State, logEntry.Exception);
if (message == null) return;
string timestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz");
string logLevel = logEntry.LogLevel.ToString().ToLower();
textWriter.WriteLine($"{timestamp} {logLevel}: {message}");
}
}