add support for loglevel color configuration

This commit is contained in:
Master 2023-10-14 20:02:51 +02:00
parent 296bdd11e5
commit 21284c69c4
5 changed files with 33 additions and 29 deletions

View File

@ -98,6 +98,7 @@ namespace AscNet.GameServer
public static void LoadPacketHandlers() public static void LoadPacketHandlers()
{ {
log.LogLevelColor[LogLevel.INFO] = ConsoleColor.White;
LoadRequestPacketHandlers(); LoadRequestPacketHandlers();
} }
@ -111,7 +112,8 @@ namespace AscNet.GameServer
foreach (var method in classes.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))) foreach (var method in classes.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)))
{ {
var attr = method.GetCustomAttribute<RequestPacketHandler>(false); var attr = method.GetCustomAttribute<RequestPacketHandler>(false);
if (attr == null || ReqHandlers.ContainsKey(attr.Name)) continue; if (attr == null || ReqHandlers.ContainsKey(attr.Name))
continue;
ReqHandlers.Add(attr.Name, (RequestPacketHandlerDelegate)Delegate.CreateDelegate(typeof(RequestPacketHandlerDelegate), method)); ReqHandlers.Add(attr.Name, (RequestPacketHandlerDelegate)Delegate.CreateDelegate(typeof(RequestPacketHandlerDelegate), method));
#if DEBUG #if DEBUG
log.Info($"Loaded {method.Name}"); log.Info($"Loaded {method.Name}");

View File

@ -23,8 +23,8 @@ namespace AscNet.GameServer
this.id = id; this.id = id;
client = tcpClient; client = tcpClient;
// TODO: add session based configuration? maybe from database? // TODO: add session based configuration? maybe from database?
log = new(typeof(Session), LogLevel.DEBUG, LogLevel.DEBUG); log = new(typeof(Session), id, LogLevel.DEBUG, LogLevel.DEBUG);
log.LogLevelColor[LogLevel.INFO] = ConsoleColor.Cyan;
Task.Run(ClientLoop); Task.Run(ClientLoop);
} }

View File

@ -0,0 +1,17 @@
namespace AscNet.Logging
{
public class LogLevelColor
{
public ConsoleColor Debug { get; set; } = ConsoleColor.Magenta;
public ConsoleColor Info { get; set; } = ConsoleColor.Green;
public ConsoleColor Warning { get; set; } = ConsoleColor.Yellow;
public ConsoleColor Error { get; set; } = ConsoleColor.Red;
public ConsoleColor Fatal { get; set; } = ConsoleColor.DarkRed;
}
}

View File

@ -15,6 +15,15 @@
#region Instantiation #region Instantiation
public Dictionary<LogLevel, ConsoleColor> LogLevelColor { get; set; } = new Dictionary<LogLevel, ConsoleColor>()
{
{ LogLevel.FATAL , ConsoleColor.DarkRed },
{ LogLevel.ERROR , ConsoleColor.Red },
{ LogLevel.WARN , ConsoleColor.Yellow },
{ LogLevel.INFO , ConsoleColor.Green },
{ LogLevel.DEBUG , ConsoleColor.Magenta }
};
public Logger(string logFilePath) public Logger(string logFilePath)
{ {
_logFilePath = logFilePath; _logFilePath = logFilePath;
@ -156,32 +165,7 @@
{ {
if (logLevel <= _logLevel) if (logLevel <= _logLevel)
{ {
switch (logLevel) Console.ForegroundColor = LogLevelColor[logLevel];
{
case LogLevel.DEBUG:
Console.ForegroundColor = ConsoleColor.Magenta;
break;
case LogLevel.INFO:
Console.ForegroundColor = ConsoleColor.Green;
break;
case LogLevel.WARN:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LogLevel.ERROR:
Console.ForegroundColor = ConsoleColor.Red;
break;
case LogLevel.FATAL:
Console.ForegroundColor = ConsoleColor.DarkRed;
break;
default:
throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null);
}
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}][{logLevel}]{message}"); Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}][{logLevel}]{message}");
Console.ResetColor(); Console.ResetColor();
} }

View File

@ -8,6 +8,7 @@ namespace AscNet.SDKServer
public static void Main(string[] args) public static void Main(string[] args)
{ {
log.LogLevelColor[Logging.LogLevel.INFO] = ConsoleColor.Blue;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Disables default logger // Disables default logger