This commit is contained in:
2023-01-27 11:25:44 +01:00
parent a3facd8bc2
commit 2630f859ea
6 changed files with 166 additions and 27 deletions

View File

@@ -2,13 +2,14 @@
<PropertyGroup>
<TargetFrameworks>
netstandard2.0;
netstandard2.1;
net5.0;
net6.0;
</TargetFrameworks>
netstandard2.0;
netstandard2.1;
net5.0;
net6.0;
net7.0;
</TargetFrameworks>
<ApplicationIcon>icon.ico</ApplicationIcon>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<Authors>EonaCat (Jeroen Saey)</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Company>EonaCat (Jeroen Saey)</Company>

View File

@@ -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;

View File

@@ -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");
}
}
}
}

View File

@@ -7,42 +7,40 @@ namespace EonaCat.Logger.Managers
/// </summary>
public class ColorSchema
{
private ConsoleColor currentForeground = Console.ForegroundColor;
/// <summary>
/// The color to use for debug messages. Default is dark gray on black.
/// The color to use for debug messages.
/// </summary>
public ColorScheme Debug = new ColorScheme(ConsoleColor.DarkGray, ConsoleColor.Black);
public ColorScheme Debug = new ColorScheme(ConsoleColor.Green, ConsoleColor.Black);
/// <summary>
/// The color to use for informational messages. Default is gray on black.
/// The color to use for informational messages.
/// </summary>
public ColorScheme Info = new ColorScheme(ConsoleColor.Gray, ConsoleColor.Black);
public ColorScheme Info = new ColorScheme(ConsoleColor.Blue, ConsoleColor.Black);
/// <summary>
/// The color to use for warning messages. Default is dark red on black.
/// The color to use for warning messages.
/// </summary>
public ColorScheme Warning = new ColorScheme(ConsoleColor.DarkRed, ConsoleColor.Black);
public ColorScheme Warning = new ColorScheme(ConsoleColor.DarkYellow, ConsoleColor.Black);
/// <summary>
/// The color to use for error messages. Default is red on black.
/// The color to use for error messages.
/// </summary>
public ColorScheme Error = new ColorScheme(ConsoleColor.Red, ConsoleColor.Black);
/// <summary>
/// The color to use for alert messages. Default is dark yellow on black.
/// The color to use for alert messages.
/// </summary>
public ColorScheme Traffic = new ColorScheme(ConsoleColor.DarkYellow, ConsoleColor.Black);
public ColorScheme Traffic = new ColorScheme(ConsoleColor.DarkMagenta, ConsoleColor.Black);
/// <summary>
/// The color to use for critical messages. Default is yellow on black.
/// The color to use for critical messages.
/// </summary>
public ColorScheme Critical = new ColorScheme(ConsoleColor.Yellow, ConsoleColor.Black);
public ColorScheme Critical = new ColorScheme(ConsoleColor.DarkRed, ConsoleColor.Black);
/// <summary>
/// The color to use for emergency messages. Default is white on red.
/// The color to use for emergency messages.
/// </summary>
public ColorScheme Trace = new ColorScheme(ConsoleColor.White, ConsoleColor.Red);
public ColorScheme Trace = new ColorScheme(ConsoleColor.Cyan, ConsoleColor.Black);
}
/// <summary>

View File

@@ -25,8 +25,9 @@ namespace EonaCat.logger.Managers
/// <param name="settings">Logger settings</param>
/// <param name="logType">logtype for the formatted message</param>
/// <param name="currentMessage">The actual message to format with the header</param>
/// <param name="dateTime">The dateTime of the message</param>
/// <returns></returns>
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;

View File

@@ -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;