2021-06-28 05:36:54 +08:00
|
|
|
using System;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-05-18 20:37:41 +08:00
|
|
|
using SimApi.Helpers;
|
2021-06-28 05:36:54 +08:00
|
|
|
|
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-03-23 07:29:43 +08:00
|
|
|
public IDisposable BeginScope<TState>(TState state) => default!;
|
|
|
|
|
|
|
|
|
|
public bool IsEnabled(LogLevel logLevel) => true;
|
|
|
|
|
|
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
|
|
|
|
|
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-04-16 06:29:21 +08:00
|
|
|
$"[ {name} ][ {DateTime.Now.ToString("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-04-16 06:29:21 +08:00
|
|
|
|
2024-07-24 21:50:46 +08:00
|
|
|
Console.WriteLine(message);
|
|
|
|
|
Console.ResetColor();
|
2021-06-28 05:36:54 +08:00
|
|
|
}
|
|
|
|
|
}
|