add part of the dialog and implement logging to console
This commit is contained in:
19
SimpleTGBot/Logging/LogLevel.cs
Normal file
19
SimpleTGBot/Logging/LogLevel.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace SimpleTGBot.Logging;
|
||||
|
||||
internal enum LogLevel : int
|
||||
{
|
||||
Debug = 0, Info, Warning, Error, Fatal
|
||||
}
|
||||
static class LogLevelExt
|
||||
{
|
||||
public static string GetName(this LogLevel level)
|
||||
{
|
||||
return level switch {
|
||||
LogLevel.Debug => "DEBUG",
|
||||
LogLevel.Info => "INFO",
|
||||
LogLevel.Warning => "WARN",
|
||||
LogLevel.Error => "ERROR",
|
||||
LogLevel.Fatal => "FATAL"
|
||||
};
|
||||
}
|
||||
}
|
||||
6
SimpleTGBot/Logging/LogSink.cs
Normal file
6
SimpleTGBot/Logging/LogSink.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace SimpleTGBot.Logging;
|
||||
|
||||
internal interface ILogSink : IDisposable
|
||||
{
|
||||
public void Log(DateTime time, LogLevel level, string message);
|
||||
}
|
||||
34
SimpleTGBot/Logging/Logger.cs
Normal file
34
SimpleTGBot/Logging/Logger.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace SimpleTGBot.Logging;
|
||||
|
||||
internal class Logger : IDisposable
|
||||
{
|
||||
public List<ILogSink> Sinks;
|
||||
|
||||
public Logger(params ILogSink[] sinks)
|
||||
{
|
||||
Sinks = new List<ILogSink>(sinks);
|
||||
}
|
||||
|
||||
public void Log(LogLevel level, string message)
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
foreach (var sink in Sinks)
|
||||
{
|
||||
sink.Log(now, level, message);
|
||||
}
|
||||
}
|
||||
|
||||
public void Debug(string message) => Log(LogLevel.Debug, message);
|
||||
public void Info(string message) => Log(LogLevel.Info, message);
|
||||
public void Warn(string message) => Log(LogLevel.Warning, message);
|
||||
public void Error(string message) => Log(LogLevel.Error, message);
|
||||
public void Fatal(string message) => Log(LogLevel.Fatal, message);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var sink in Sinks)
|
||||
{
|
||||
sink.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
23
SimpleTGBot/Logging/StdoutSink.cs
Normal file
23
SimpleTGBot/Logging/StdoutSink.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
namespace SimpleTGBot.Logging;
|
||||
|
||||
internal class StdoutSink : ILogSink
|
||||
{
|
||||
private readonly ConsoleColor[] colors = [ConsoleColor.White, ConsoleColor.Cyan, ConsoleColor.Yellow, ConsoleColor.DarkRed, ConsoleColor.Red];
|
||||
private ConsoleColor originalConsoleColor;
|
||||
|
||||
public StdoutSink()
|
||||
{
|
||||
originalConsoleColor = Console.ForegroundColor;
|
||||
}
|
||||
|
||||
public void Log(DateTime time, LogLevel level, string message)
|
||||
{
|
||||
Console.ForegroundColor = colors[(int)level];
|
||||
foreach (string line in message.Split(Environment.NewLine))
|
||||
Console.WriteLine($"({time:u}) [{level.GetName()}] {line}");
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Console.ForegroundColor = originalConsoleColor;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user