mirror of
https://github.com/zs-yg/MCSJ.git
synced 2025-12-06 10:50:42 +08:00
添加日志系统和gitignore
This commit is contained in:
29
Tools/log_systeam/log_creator.cs
Normal file
29
Tools/log_systeam/log_creator.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace MCSJ.Tools.LogSystem
|
||||
{
|
||||
public static class LogCreator
|
||||
{
|
||||
public static string GenerateLogFileName()
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
return $"{now:yyyy-MM-dd-HH-mm-ss}.log";
|
||||
}
|
||||
|
||||
public static string GetLogDirectory()
|
||||
{
|
||||
string logDir = Path.Combine(Directory.GetCurrentDirectory(), "logs");
|
||||
if (!Directory.Exists(logDir))
|
||||
{
|
||||
Directory.CreateDirectory(logDir);
|
||||
}
|
||||
return logDir;
|
||||
}
|
||||
|
||||
public static string GetLogFilePath()
|
||||
{
|
||||
return Path.Combine(GetLogDirectory(), GenerateLogFileName());
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Tools/log_systeam/log_main.cs
Normal file
37
Tools/log_systeam/log_main.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace MCSJ.Tools.LogSystem
|
||||
{
|
||||
public static class LogMain
|
||||
{
|
||||
private static readonly object _lock = new object();
|
||||
|
||||
public enum LogLevel
|
||||
{
|
||||
DEBUG,
|
||||
INFO,
|
||||
WARN,
|
||||
ERROR,
|
||||
FATAL
|
||||
}
|
||||
|
||||
public static void Log(LogLevel level, string message)
|
||||
{
|
||||
string logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{level}] {message}";
|
||||
string logPath = LogCreator.GetLogFilePath();
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
File.AppendAllText(logPath, logEntry + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Debug(string message) => Log(LogLevel.DEBUG, message);
|
||||
public static void Info(string message) => Log(LogLevel.INFO, message);
|
||||
public static void Warn(string message) => Log(LogLevel.WARN, message);
|
||||
public static void Error(string message) => Log(LogLevel.ERROR, message);
|
||||
public static void Fatal(string message) => Log(LogLevel.FATAL, message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user