mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 11:37:01 +00:00
日志系统
This commit is contained in:
45
Logger/LogEntry.cs
Normal file
45
Logger/LogEntry.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
|
||||
namespace CMLeonOS.Logger
|
||||
{
|
||||
public class LogEntry
|
||||
{
|
||||
public DateTime Timestamp;
|
||||
public LogLevel Level;
|
||||
public string Source;
|
||||
public string Message;
|
||||
|
||||
public LogEntry(LogLevel level, string source, string message)
|
||||
{
|
||||
Timestamp = DateTime.Now;
|
||||
Level = level;
|
||||
Source = source;
|
||||
Message = message;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string levelStr = GetLevelString(Level);
|
||||
return "[" + levelStr + "] [" + Source + "] " + Message;
|
||||
}
|
||||
|
||||
private string GetLevelString(LogLevel level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case LogLevel.Debug:
|
||||
return "DEBUG";
|
||||
case LogLevel.Info:
|
||||
return "INFO";
|
||||
case LogLevel.Warning:
|
||||
return "WARN";
|
||||
case LogLevel.Error:
|
||||
return "ERROR";
|
||||
case LogLevel.Success:
|
||||
return "SUCCESS";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Logger/LogLevel.cs
Normal file
11
Logger/LogLevel.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace CMLeonOS.Logger
|
||||
{
|
||||
public enum LogLevel
|
||||
{
|
||||
Debug,
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
Success
|
||||
}
|
||||
}
|
||||
109
Logger/Logger.cs
Normal file
109
Logger/Logger.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
|
||||
namespace CMLeonOS.Logger
|
||||
{
|
||||
public class Logger
|
||||
{
|
||||
private static Logger _instance;
|
||||
|
||||
private LogLevel _minLogLevel;
|
||||
private bool _enableConsoleOutput;
|
||||
|
||||
private Logger()
|
||||
{
|
||||
_minLogLevel = LogLevel.Info;
|
||||
_enableConsoleOutput = true;
|
||||
}
|
||||
|
||||
public static Logger Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = new Logger();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public LogLevel MinLogLevel
|
||||
{
|
||||
get { return _minLogLevel; }
|
||||
set { _minLogLevel = value; }
|
||||
}
|
||||
|
||||
public bool EnableConsoleOutput
|
||||
{
|
||||
get { return _enableConsoleOutput; }
|
||||
set { _enableConsoleOutput = value; }
|
||||
}
|
||||
|
||||
public void Log(LogLevel level, string source, string message)
|
||||
{
|
||||
if (level < _minLogLevel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var entry = new LogEntry(level, source, message);
|
||||
|
||||
if (_enableConsoleOutput)
|
||||
{
|
||||
WriteToConsole(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public void Debug(string source, string message)
|
||||
{
|
||||
Log(LogLevel.Debug, source, message);
|
||||
}
|
||||
|
||||
public void Info(string source, string message)
|
||||
{
|
||||
Log(LogLevel.Info, source, message);
|
||||
}
|
||||
|
||||
public void Warning(string source, string message)
|
||||
{
|
||||
Log(LogLevel.Warning, source, message);
|
||||
}
|
||||
|
||||
public void Error(string source, string message)
|
||||
{
|
||||
Log(LogLevel.Error, source, message);
|
||||
}
|
||||
|
||||
public void Success(string source, string message)
|
||||
{
|
||||
Log(LogLevel.Success, source, message);
|
||||
}
|
||||
|
||||
private void WriteToConsole(LogEntry entry)
|
||||
{
|
||||
ConsoleColor originalColor = Console.ForegroundColor;
|
||||
|
||||
switch (entry.Level)
|
||||
{
|
||||
case LogLevel.Debug:
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
break;
|
||||
case LogLevel.Info:
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
break;
|
||||
case LogLevel.Warning:
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
break;
|
||||
case LogLevel.Error:
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
break;
|
||||
case LogLevel.Success:
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine(entry.ToString());
|
||||
Console.ForegroundColor = originalColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user