dragon-iso/src/TeamsISO.Engine/Logging/EngineLogging.cs

35 lines
1.4 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Extensions.Logging;
namespace TeamsISO.Engine.Logging;
/// <summary>
/// Convenience factory for an <see cref="ILoggerFactory"/> wired to Serilog's console sink.
/// Phase A wires console-only; Phase C will add the rolling-file sink under %APPDATA%\TeamsISO\logs\.
/// </summary>
public static class EngineLogging
{
public static ILoggerFactory CreateConsole(LogLevel minimum = LogLevel.Information)
{
var serilog = new LoggerConfiguration()
.MinimumLevel.Is(MapLevel(minimum))
.Enrich.WithProperty("Component", "TeamsISO.Engine")
.WriteTo.Console(outputTemplate:
"[{Timestamp:HH:mm:ss} {Level:u3}] [{Component}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
return new SerilogLoggerFactory(serilog, dispose: true);
}
private static Serilog.Events.LogEventLevel MapLevel(LogLevel level) => level switch
{
LogLevel.Trace => Serilog.Events.LogEventLevel.Verbose,
LogLevel.Debug => Serilog.Events.LogEventLevel.Debug,
LogLevel.Information => Serilog.Events.LogEventLevel.Information,
LogLevel.Warning => Serilog.Events.LogEventLevel.Warning,
LogLevel.Error => Serilog.Events.LogEventLevel.Error,
LogLevel.Critical => Serilog.Events.LogEventLevel.Fatal,
_ => Serilog.Events.LogEventLevel.Information
};
}