Files
simapi-net/Logger/SimApiLogger.cs
T

42 lines
1.2 KiB
C#
Raw Normal View History

2021-06-28 05:36:54 +08:00
using System;
using Microsoft.Extensions.Logging;
2024-03-23 07:29:43 +08:00
namespace SimApi.Logger;
public class SimApiLogger(string name) : ILogger
2021-06-28 05:36:54 +08:00
{
2024-08-03 19:19:42 +08:00
public IDisposable BeginScope<TState>(TState state) where TState : notnull => default!;
2024-03-23 07:29:43 +08:00
public bool IsEnabled(LogLevel logLevel) => true;
2024-07-26 08:11:21 +08:00
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
2024-03-23 07:29:43 +08:00
Func<TState, Exception, string> formatter)
2021-06-28 05:36:54 +08:00
{
2024-07-24 21:50:46 +08:00
Console.ForegroundColor = logLevel switch
2021-06-28 05:36:54 +08:00
{
2024-03-23 07:29:43 +08:00
LogLevel.Debug => ConsoleColor.DarkMagenta,
LogLevel.Information => ConsoleColor.DarkCyan,
LogLevel.Warning => ConsoleColor.Yellow,
LogLevel.Error => ConsoleColor.Red,
LogLevel.Critical => ConsoleColor.DarkRed,
_ => ConsoleColor.White
};
var message =
2024-07-26 08:11:21 +08:00
$"[ {name} ][ {DateTime.Now:yyyy-MM-dd HH:mm:ss:ffff} ][ {logLevel.ToString()} ]\n{state}\n";
2024-03-23 07:29:43 +08:00
if (exception != null)
2021-06-28 05:36:54 +08:00
{
2024-03-23 07:29:43 +08:00
message += $"{exception}\n";
2021-06-28 05:36:54 +08:00
}
2024-07-24 21:50:46 +08:00
Console.WriteLine(message);
Console.ResetColor();
2021-06-28 05:36:54 +08:00
}
2024-08-03 19:19:42 +08:00
}
public class EmptyDisposable : IDisposable
{
public static EmptyDisposable Instance { get; } = new EmptyDisposable();
public void Dispose()
{
}
2021-06-28 05:36:54 +08:00
}