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
|
|
|
|
|
|
|
|
namespace SimApi.Logger
|
|
|
|
|
{
|
|
|
|
|
public class SimApiLogger : ILogger
|
|
|
|
|
{
|
|
|
|
|
private string Name { get; }
|
|
|
|
|
|
|
|
|
|
public SimApiLogger(string name)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-07 21:30:45 +08:00
|
|
|
public IDisposable BeginScope<TState>(TState state) => default!;
|
2021-06-28 05:36:54 +08:00
|
|
|
|
2022-05-07 21:30:45 +08:00
|
|
|
public bool IsEnabled(LogLevel logLevel) => true;
|
2021-06-28 05:36:54 +08:00
|
|
|
|
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
|
|
|
|
|
Func<TState, Exception, string> formatter)
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = logLevel switch
|
|
|
|
|
{
|
|
|
|
|
LogLevel.Debug => ConsoleColor.DarkMagenta,
|
|
|
|
|
LogLevel.Information => ConsoleColor.DarkCyan,
|
|
|
|
|
LogLevel.Warning => ConsoleColor.Yellow,
|
|
|
|
|
LogLevel.Error => ConsoleColor.Red,
|
|
|
|
|
LogLevel.Critical => ConsoleColor.DarkRed,
|
2023-09-26 20:47:41 +08:00
|
|
|
_ => ConsoleColor.White
|
2021-06-28 05:36:54 +08:00
|
|
|
};
|
2023-09-26 20:47:41 +08:00
|
|
|
var message =
|
|
|
|
|
$"[ {Name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n";
|
2021-06-28 05:36:54 +08:00
|
|
|
if (exception != null)
|
|
|
|
|
{
|
2023-09-26 20:47:41 +08:00
|
|
|
message += $"{exception}\n";
|
2021-06-28 05:36:54 +08:00
|
|
|
}
|
2023-09-26 20:47:41 +08:00
|
|
|
Console.WriteLine(message);
|
2021-06-28 05:36:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|