From 21284c69c45a70bf3bdee6d0e3abd189509b9bb1 Mon Sep 17 00:00:00 2001 From: Master Date: Sat, 14 Oct 2023 20:02:51 +0200 Subject: [PATCH] add support for loglevel color configuration --- AscNet.GameServer/Packet.cs | 4 +++- AscNet.GameServer/Session.cs | 4 ++-- AscNet.Logging/LogLevelColor.cs | 17 ++++++++++++++++ AscNet.Logging/Logger.cs | 36 +++++++++------------------------ AscNet.SDKServer/SDKServer.cs | 1 + 5 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 AscNet.Logging/LogLevelColor.cs diff --git a/AscNet.GameServer/Packet.cs b/AscNet.GameServer/Packet.cs index 5725b9e2..d0877875 100644 --- a/AscNet.GameServer/Packet.cs +++ b/AscNet.GameServer/Packet.cs @@ -98,6 +98,7 @@ namespace AscNet.GameServer public static void LoadPacketHandlers() { + log.LogLevelColor[LogLevel.INFO] = ConsoleColor.White; LoadRequestPacketHandlers(); } @@ -111,7 +112,8 @@ namespace AscNet.GameServer foreach (var method in classes.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))) { var attr = method.GetCustomAttribute(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)); #if DEBUG log.Info($"Loaded {method.Name}"); diff --git a/AscNet.GameServer/Session.cs b/AscNet.GameServer/Session.cs index 994a9ad5..22a4f4cd 100644 --- a/AscNet.GameServer/Session.cs +++ b/AscNet.GameServer/Session.cs @@ -23,8 +23,8 @@ namespace AscNet.GameServer this.id = id; client = tcpClient; // 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); } diff --git a/AscNet.Logging/LogLevelColor.cs b/AscNet.Logging/LogLevelColor.cs new file mode 100644 index 00000000..aa8b94ee --- /dev/null +++ b/AscNet.Logging/LogLevelColor.cs @@ -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; + + } +} diff --git a/AscNet.Logging/Logger.cs b/AscNet.Logging/Logger.cs index 91356953..c171907c 100644 --- a/AscNet.Logging/Logger.cs +++ b/AscNet.Logging/Logger.cs @@ -15,6 +15,15 @@ #region Instantiation + public Dictionary LogLevelColor { get; set; } = new Dictionary() + { + { LogLevel.FATAL , ConsoleColor.DarkRed }, + { LogLevel.ERROR , ConsoleColor.Red }, + { LogLevel.WARN , ConsoleColor.Yellow }, + { LogLevel.INFO , ConsoleColor.Green }, + { LogLevel.DEBUG , ConsoleColor.Magenta } + }; + public Logger(string logFilePath) { _logFilePath = logFilePath; @@ -156,32 +165,7 @@ { if (logLevel <= _logLevel) { - switch (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.ForegroundColor = LogLevelColor[logLevel]; Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}][{logLevel}]{message}"); Console.ResetColor(); } diff --git a/AscNet.SDKServer/SDKServer.cs b/AscNet.SDKServer/SDKServer.cs index 082481b1..2a1a1d2d 100644 --- a/AscNet.SDKServer/SDKServer.cs +++ b/AscNet.SDKServer/SDKServer.cs @@ -8,6 +8,7 @@ namespace AscNet.SDKServer public static void Main(string[] args) { + log.LogLevelColor[Logging.LogLevel.INFO] = ConsoleColor.Blue; var builder = WebApplication.CreateBuilder(args); // Disables default logger