This commit is contained in:
30
BotPages.Core/Logging/ConsoleLogger.cs
Normal file
30
BotPages.Core/Logging/ConsoleLogger.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace BotPages.Core.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Вывод лога в консоль.
|
||||
/// </summary>
|
||||
public sealed class ConsoleLogger : ILogger
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Log(LogLevel level, string message, Exception? ex = null)
|
||||
{
|
||||
var prefix = level switch
|
||||
{
|
||||
LogLevel.Info => "[INFO] ",
|
||||
LogLevel.Warn => "[WARN] ",
|
||||
LogLevel.Critical => "[CRIT] ",
|
||||
_ => "[LOG] "
|
||||
};
|
||||
|
||||
string text = $"{DateTime.UtcNow:O} {prefix}{message}{(ex is null ? "" : $" :: {ex.Message}")}";
|
||||
|
||||
if (level == LogLevel.Critical)
|
||||
{
|
||||
Console.Error.WriteLine(text);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
BotPages.Core/Logging/ILogger.cs
Normal file
15
BotPages.Core/Logging/ILogger.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace BotPages.Core.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Интерфейс логгера для BotPages.
|
||||
/// </summary>
|
||||
public interface ILogger
|
||||
{
|
||||
/// <summary>
|
||||
/// Записать сообщение в лог.
|
||||
/// </summary>
|
||||
/// <param name="level">Уровень логирования.</param>
|
||||
/// <param name="message">Текст сообщения.</param>
|
||||
/// <param name="ex">Исключение, если есть.</param>
|
||||
void Log(LogLevel level, string message, Exception? ex = null);
|
||||
}
|
||||
14
BotPages.Core/Logging/LogLevel.cs
Normal file
14
BotPages.Core/Logging/LogLevel.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace BotPages.Core.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Уровни логирования для BotPages.
|
||||
/// </summary>
|
||||
public enum LogLevel
|
||||
{
|
||||
/// <summary>Информационные сообщения.</summary>
|
||||
Info,
|
||||
/// <summary>Предупреждения (например, деградация возможностей).</summary>
|
||||
Warn,
|
||||
/// <summary>Критические ошибки, делающие невозможным продолжение работы.</summary>
|
||||
Critical
|
||||
}
|
||||
12
BotPages.Core/Logging/WithoutLogger.cs
Normal file
12
BotPages.Core/Logging/WithoutLogger.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace BotPages.Core.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Отключение логирования
|
||||
/// </summary>
|
||||
public sealed class WithoutLogger : ILogger
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Log(LogLevel level, string message, Exception? ex = null)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user