From 2630f859ea15c97e1d72e2379b2d0f7a40672988 Mon Sep 17 00:00:00 2001 From: EonaCat Date: Fri, 27 Jan 2023 11:25:44 +0100 Subject: [PATCH] Updated --- EonaCat.Logger/EonaCat.Logger.csproj | 13 +- .../Internal/BatchingLogger.cs | 2 +- EonaCat.Logger/Helpers/ColorHelper.cs | 135 ++++++++++++++++++ EonaCat.Logger/Managers/ColorSchema.cs | 28 ++-- EonaCat.Logger/Managers/LogHelper.cs | 6 +- EonaCat.Logger/Managers/LogManager.cs | 9 +- 6 files changed, 166 insertions(+), 27 deletions(-) create mode 100644 EonaCat.Logger/Helpers/ColorHelper.cs diff --git a/EonaCat.Logger/EonaCat.Logger.csproj b/EonaCat.Logger/EonaCat.Logger.csproj index 9ddf542..961f24a 100644 --- a/EonaCat.Logger/EonaCat.Logger.csproj +++ b/EonaCat.Logger/EonaCat.Logger.csproj @@ -2,13 +2,14 @@ - netstandard2.0; - netstandard2.1; - net5.0; - net6.0; - + netstandard2.0; + netstandard2.1; + net5.0; + net6.0; + net7.0; + icon.ico - 1.0.4 + 1.0.5 EonaCat (Jeroen Saey) true EonaCat (Jeroen Saey) diff --git a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs index b686334..4dcaf57 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs @@ -42,7 +42,7 @@ namespace EonaCat.Logger.Internal _loggerSettings = new LoggerSettings(); } - var message = LogHelper.FormatMessageWithHeader(_loggerSettings, logLevel.FromLogLevel(), formatter(state, exception)) + Environment.NewLine; + var message = LogHelper.FormatMessageWithHeader(_loggerSettings, logLevel.FromLogLevel(), formatter(state, exception), DateTime.Now) + Environment.NewLine; if (exception != null) { message = exception.FormatExceptionToMessage() + Environment.NewLine; diff --git a/EonaCat.Logger/Helpers/ColorHelper.cs b/EonaCat.Logger/Helpers/ColorHelper.cs new file mode 100644 index 0000000..63d5422 --- /dev/null +++ b/EonaCat.Logger/Helpers/ColorHelper.cs @@ -0,0 +1,135 @@ +using System; +using System.Drawing; +using System.Globalization; + +namespace EonaCat.Logger.Helpers +{ + public static class ColorHelper + { + public static string ColorToHexString(Color c) + { + return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); + } + + public static string ColorToRGBString(Color c) + { + return "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")"; + } + + public static Color ConsoleColorToColor(this ConsoleColor consoleColor) + { + switch (consoleColor) + { + case ConsoleColor.Black: + return Color.Black; + + case ConsoleColor.DarkBlue: + return HexStringToColor("#000080"); + + case ConsoleColor.DarkGreen: + return HexStringToColor("#008000"); + + case ConsoleColor.DarkCyan: + return HexStringToColor("#008080"); + + case ConsoleColor.DarkRed: + return HexStringToColor("#800000"); + + case ConsoleColor.DarkMagenta: + return HexStringToColor("#800080"); + + case ConsoleColor.DarkYellow: + return HexStringToColor("#808000"); + + case ConsoleColor.Gray: + return HexStringToColor("#C0C0C0"); + + case ConsoleColor.DarkGray: + return HexStringToColor("#808080"); + + case ConsoleColor.Blue: + return Color.Blue; + + case ConsoleColor.Green: + return Color.Lime; + + case ConsoleColor.Cyan: + return Color.Cyan; + + case ConsoleColor.Red: + return Color.Red; + + case ConsoleColor.Magenta: + return Color.Magenta; + + case ConsoleColor.Yellow: + return Color.Yellow; + + case ConsoleColor.White: + return Color.White; + + default: + throw new NotSupportedException(); + } + } + + public static Color HexStringToColor(string htmlColor, bool requireHexSpecified = false, int defaultAlpha = 0xFF) => Color.FromArgb(HexColorToArgb(htmlColor, requireHexSpecified, defaultAlpha)); + + public static int HexColorToArgb(string htmlColor, bool requireHexSpecified = false, int defaultAlpha = 0xFF) + { + if (string.IsNullOrEmpty(htmlColor)) + { + throw new ArgumentNullException(nameof(htmlColor)); + } + + if (!htmlColor.StartsWith("#") && requireHexSpecified) + { + throw new ArgumentException($"Provided parameter '{htmlColor}' is not valid"); + } + + htmlColor = htmlColor.TrimStart('#'); + + + var symbolCount = htmlColor.Length; + var value = int.Parse(htmlColor, NumberStyles.HexNumber); + switch (symbolCount) + { + case 3: // RGB short hand + { + return defaultAlpha << 24 + | value & 0xF + | (value & 0xF) << 4 + | (value & 0xF0) << 4 + | (value & 0xF0) << 8 + | (value & 0xF00) << 8 + | (value & 0xF00) << 12 + ; + } + case 4: // RGBA short hand + { + // Inline alpha swap + return (value & 0xF) << 24 + | (value & 0xF) << 28 + | (value & 0xF0) >> 4 + | value & 0xF0 + | value & 0xF00 + | (value & 0xF00) << 4 + | (value & 0xF000) << 4 + | (value & 0xF000) << 8 + ; + } + case 6: // RGB complete definition + { + return defaultAlpha << 24 | value; + } + case 8: // RGBA complete definition + { + // Alpha swap + return (value & 0xFF) << 24 | value >> 8; + } + default: + throw new FormatException("Invalid HTML Color"); + } + } + } +} diff --git a/EonaCat.Logger/Managers/ColorSchema.cs b/EonaCat.Logger/Managers/ColorSchema.cs index 0c65962..f40c19e 100644 --- a/EonaCat.Logger/Managers/ColorSchema.cs +++ b/EonaCat.Logger/Managers/ColorSchema.cs @@ -7,42 +7,40 @@ namespace EonaCat.Logger.Managers /// public class ColorSchema { - private ConsoleColor currentForeground = Console.ForegroundColor; - /// - /// The color to use for debug messages. Default is dark gray on black. + /// The color to use for debug messages. /// - public ColorScheme Debug = new ColorScheme(ConsoleColor.DarkGray, ConsoleColor.Black); + public ColorScheme Debug = new ColorScheme(ConsoleColor.Green, ConsoleColor.Black); /// - /// The color to use for informational messages. Default is gray on black. + /// The color to use for informational messages. /// - public ColorScheme Info = new ColorScheme(ConsoleColor.Gray, ConsoleColor.Black); + public ColorScheme Info = new ColorScheme(ConsoleColor.Blue, ConsoleColor.Black); /// - /// The color to use for warning messages. Default is dark red on black. + /// The color to use for warning messages. /// - public ColorScheme Warning = new ColorScheme(ConsoleColor.DarkRed, ConsoleColor.Black); + public ColorScheme Warning = new ColorScheme(ConsoleColor.DarkYellow, ConsoleColor.Black); /// - /// The color to use for error messages. Default is red on black. + /// The color to use for error messages. /// public ColorScheme Error = new ColorScheme(ConsoleColor.Red, ConsoleColor.Black); /// - /// The color to use for alert messages. Default is dark yellow on black. + /// The color to use for alert messages. /// - public ColorScheme Traffic = new ColorScheme(ConsoleColor.DarkYellow, ConsoleColor.Black); + public ColorScheme Traffic = new ColorScheme(ConsoleColor.DarkMagenta, ConsoleColor.Black); /// - /// The color to use for critical messages. Default is yellow on black. + /// The color to use for critical messages. /// - public ColorScheme Critical = new ColorScheme(ConsoleColor.Yellow, ConsoleColor.Black); + public ColorScheme Critical = new ColorScheme(ConsoleColor.DarkRed, ConsoleColor.Black); /// - /// The color to use for emergency messages. Default is white on red. + /// The color to use for emergency messages. /// - public ColorScheme Trace = new ColorScheme(ConsoleColor.White, ConsoleColor.Red); + public ColorScheme Trace = new ColorScheme(ConsoleColor.Cyan, ConsoleColor.Black); } /// diff --git a/EonaCat.Logger/Managers/LogHelper.cs b/EonaCat.Logger/Managers/LogHelper.cs index 6f74fee..2479abb 100644 --- a/EonaCat.Logger/Managers/LogHelper.cs +++ b/EonaCat.Logger/Managers/LogHelper.cs @@ -25,8 +25,9 @@ namespace EonaCat.logger.Managers /// Logger settings /// logtype for the formatted message /// The actual message to format with the header + /// The dateTime of the message /// - public static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage) + public static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage, DateTime dateTime) { if (string.IsNullOrWhiteSpace(currentMessage)) { @@ -41,7 +42,7 @@ namespace EonaCat.logger.Managers string header = settings.HeaderFormat; if (header.Contains("{ts}")) { - header = header.Replace("{ts}", DateTime.Now.ToUniversalTime().ToString(settings.TimestampFormat)); + header = header.Replace("{ts}", dateTime.ToString(settings.TimestampFormat)); } if (header.Contains("{host}")) @@ -209,7 +210,6 @@ namespace EonaCat.logger.Managers if (settings == null) return; if (!settings.EnableFileLogging) return; if (string.IsNullOrWhiteSpace(message)) return; - //logger.LogInformation(message); int tries = 0; bool completed = false; diff --git a/EonaCat.Logger/Managers/LogManager.cs b/EonaCat.Logger/Managers/LogManager.cs index feb9872..dd8ab1a 100644 --- a/EonaCat.Logger/Managers/LogManager.cs +++ b/EonaCat.Logger/Managers/LogManager.cs @@ -169,7 +169,7 @@ namespace EonaCat.Logger.Managers currentMessage = message; } - var fullMessage = LogHelper.FormatMessageWithHeader(_settings, logType, currentMessage); + var fullMessage = LogHelper.FormatMessageWithHeader(_settings, logType, currentMessage, dateTime); LogHelper.SendConsole(_settings, logType, fullMessage); @@ -300,7 +300,12 @@ namespace EonaCat.Logger.Managers logLevel = DllInfo.LogLevel; } - if (logType == ELogType.CRITICAL || logLevel.GetValueOrDefault() <= logType) + if (logType == ELogType.NONE) + { + return; + } + + if (logType == ELogType.CRITICAL || logType <= logLevel.GetValueOrDefault()) { DateTime now = DateTime.Now;