Production hardening: kill switch, circuit breaker, trailing stops, log level, holiday calendar
Some checks failed
Build and Test / build (push) Has been cancelled
Some checks failed
Build and Test / build (push) Has been cancelled
This commit is contained in:
@@ -2,6 +2,18 @@ using System;
|
||||
|
||||
namespace NT8.Core.Logging
|
||||
{
|
||||
/// <summary>
|
||||
/// Log severity levels.
|
||||
/// </summary>
|
||||
public enum LogLevel
|
||||
{
|
||||
Debug = 0,
|
||||
Information = 1,
|
||||
Warning = 2,
|
||||
Error = 3,
|
||||
Critical = 4
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Basic console logger implementation for .NET Framework 4.8
|
||||
/// </summary>
|
||||
@@ -9,43 +21,53 @@ namespace NT8.Core.Logging
|
||||
{
|
||||
private readonly string _categoryName;
|
||||
|
||||
/// <summary>
|
||||
/// Minimum log level to write. Messages below this level are suppressed.
|
||||
/// Default is Information.
|
||||
/// </summary>
|
||||
public LogLevel MinimumLevel { get; set; }
|
||||
|
||||
public BasicLogger(string categoryName = "")
|
||||
{
|
||||
_categoryName = categoryName;
|
||||
MinimumLevel = LogLevel.Information;
|
||||
}
|
||||
|
||||
public void LogDebug(string message, params object[] args)
|
||||
{
|
||||
WriteLog("DEBUG", message, args);
|
||||
WriteLog(LogLevel.Debug, "DEBUG", message, args);
|
||||
}
|
||||
|
||||
public void LogInformation(string message, params object[] args)
|
||||
{
|
||||
WriteLog("INFO", message, args);
|
||||
WriteLog(LogLevel.Information, "INFO", message, args);
|
||||
}
|
||||
|
||||
public void LogWarning(string message, params object[] args)
|
||||
{
|
||||
WriteLog("WARN", message, args);
|
||||
WriteLog(LogLevel.Warning, "WARN", message, args);
|
||||
}
|
||||
|
||||
public void LogError(string message, params object[] args)
|
||||
{
|
||||
WriteLog("ERROR", message, args);
|
||||
WriteLog(LogLevel.Error, "ERROR", message, args);
|
||||
}
|
||||
|
||||
public void LogCritical(string message, params object[] args)
|
||||
{
|
||||
WriteLog("CRITICAL", message, args);
|
||||
WriteLog(LogLevel.Critical, "CRITICAL", message, args);
|
||||
}
|
||||
|
||||
private void WriteLog(string level, string message, params object[] args)
|
||||
private void WriteLog(LogLevel level, string levelLabel, string message, params object[] args)
|
||||
{
|
||||
if (level < MinimumLevel)
|
||||
return;
|
||||
|
||||
var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
var formattedMessage = args.Length > 0 ? String.Format(message, args) : message;
|
||||
var category = !String.IsNullOrEmpty(_categoryName) ? String.Format("[{0}] ", _categoryName) : "";
|
||||
|
||||
Console.WriteLine(String.Format("{0} [{1}] {2}{3}", timestamp, level, category, formattedMessage));
|
||||
|
||||
Console.WriteLine(String.Format("{0} [{1}] {2}{3}", timestamp, levelLabel, category, formattedMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user