added colors to ConsoleLogger

This commit is contained in:
Timerix22 2022-11-17 21:19:01 +06:00
parent db18bbe790
commit 7397933601
3 changed files with 41 additions and 29 deletions

View File

@ -17,12 +17,21 @@ public class ConsoleLogger : ILogger
{
var msg = format.CreateMessage(context, severity, message);
lock (consolelocker)
ColoredConsole.Write(msg);
ColoredConsole.Write(ColorFromSeverity(severity),msg);
}
public void Log(string context, LogSeverity severity, object message)
=> Log(context, severity, message, Format);
private static ConsoleColor ColorFromSeverity(LogSeverity severity)
=> severity switch
{
LogSeverity.Debug => ConsoleColor.Gray,
LogSeverity.Info => ConsoleColor.White,
LogSeverity.Warn => ConsoleColor.Yellow,
LogSeverity.Error => ConsoleColor.Red,
_ => throw new ArgumentOutOfRangeException(nameof(severity), severity, null)
};
public void Dispose()
{

View File

@ -9,8 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTLib.Tests", "DTLib.Tests\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6308F24E-A4FF-46B3-B72F-30E05DDCB1D5}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
README.md = README.md
.gitignore = .gitignore
.gitmodules = .gitmodules
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTLib.Dtsod", "DTLib.Dtsod\DTLib.Dtsod.csproj", "{ADE425F5-8645-47F0-9AA8-33FA748D36BE}"

View File

@ -30,36 +30,36 @@ public static class ColoredConsole
_ => throw new Exception($"ColoredConsole.ParseColor({color}) error: incorrect color"),
};
public static void Write(ConsoleColor color,string msg)
{
Console.ForegroundColor = color;
Console.Write(msg);
Console.ForegroundColor = ConsoleColor.Gray;
}
public static void Write(string msg) => Write(ConsoleColor.Gray, msg);
// вывод цветного текста
public static void Write(params string[] input)
{
if (input.Length == 1)
{
if (Console.ForegroundColor != ConsoleColor.Gray)
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(input[0]);
}
else if (input.Length % 2 == 0)
{
StringBuilder strB = new();
if (input.Length % 2 != 0)
throw new Exception("ColoredConsole.Write() error: every text string must have color string before");
for (ushort i = 0; i < input.Length; i++)
{
ConsoleColor c = ParseColor(input[i]);
if (Console.ForegroundColor != c)
{
Console.Write(strB.ToString());
Console.ForegroundColor = c;
strB.Clear();
Console.ForegroundColor = ParseColor(input[i++]);
Console.Write(input[i]);
}
strB.Append(input[++i]);
}
if (strB.Length > 0)
Console.Write(strB.ToString());
}
else throw new Exception("ColoredConsole.Write() error: every text string must have color string before");
Console.ForegroundColor = ConsoleColor.Gray;
}
public static void WriteLine() => Console.WriteLine();
public static void WriteLine(ConsoleColor color,string msg)
{
Console.ForegroundColor = color;
Console.WriteLine(msg);
Console.ForegroundColor = ConsoleColor.Gray;
}
public static void WriteLine(params string[] input)
{
@ -68,11 +68,13 @@ public static class ColoredConsole
}
// ввод цветного текста
public static string Read(string color)
public static string Read(ConsoleColor color)
{
ConsoleColor c = ParseColor(color);
if (Console.ForegroundColor != c)
Console.ForegroundColor = c;
return Console.ReadLine();
Console.ForegroundColor = color;
var r = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Gray;
return r;
}
public static string Read(string color) => Read(ParseColor(color));
}