From 006f224efd6c9b2325a55dea047ddcc2dfdd92e3 Mon Sep 17 00:00:00 2001 From: Muihd <1083092844@qq.com> Date: Sun, 10 Mar 2024 03:42:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=20ColorConsole=20=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=EF=BC=8C=E5=85=A8=E9=83=A8=E6=94=B9=E7=94=A8Logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BarrageGrab/Logger.cs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/BarrageGrab/Logger.cs b/BarrageGrab/Logger.cs index aae19aa..32cd6bc 100644 --- a/BarrageGrab/Logger.cs +++ b/BarrageGrab/Logger.cs @@ -15,6 +15,22 @@ namespace BarrageGrab private static readonly NLog.Logger logger = builder.GetLogger("*"); + public static void PrintColor(string message, ConsoleColor foreground = ConsoleColor.White) + { + var color = Console.ForegroundColor; + Console.ForegroundColor = foreground; + Console.WriteLine(message); + Console.ForegroundColor = color; + } + + public static void LogSucc(string message) + { + var color = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Green; + logger.Info(message); + Console.ForegroundColor = color; + } + // 记录日志方法 public static void LogTrace(string message) { @@ -33,27 +49,42 @@ namespace BarrageGrab public static void LogWarn(string message) { + var color = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Yellow; logger.Warn(message); + Console.ForegroundColor = color; } public static void LogError(string message) { + var color = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; logger.Error(message); + Console.ForegroundColor = color; } public static void LogError(Exception ex, string message) { + var color = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; logger.Error(ex, message); + Console.ForegroundColor = color; } public static void LogFatal(string message) { + var color = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; logger.Fatal(message); + Console.ForegroundColor = color; } public static void LogFatal(Exception ex, string message) { + var color = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; logger.Fatal(ex, message); + Console.ForegroundColor = color; } /// @@ -148,7 +179,7 @@ namespace BarrageGrab if (++count > maxCount) { //删除最早的文件 - var first = fiels.Select(s=>new FileInfo(s)).OrderBy(o=>o.CreationTime).FirstOrDefault(); + var first = fiels.Select(s => new FileInfo(s)).OrderBy(o => o.CreationTime).FirstOrDefault(); File.Delete(first.FullName); } }