添加日志系统和gitignore

This commit is contained in:
zs-yg
2025-11-09 14:48:42 +08:00
parent 0b6f66304b
commit 8982aa9bcc
4 changed files with 83 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
bin/
obj/
log/

View File

@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using MCSJ.Tools;
using MCSJ.Tools.LogSystem;
namespace MCSJ
{
@@ -8,8 +9,15 @@ namespace MCSJ
{
static async Task Main(string[] args)
{
// 验证日志目录
string logDir = LogCreator.GetLogDirectory();
Console.WriteLine($"日志目录: {logDir}");
LogMain.Debug($"日志文件: {LogCreator.GetLogFilePath()}");
LogMain.Info("MC服务器下载工具启动");
var versionManager = new VersionManager();
var downloadService = new DownloadService(versionManager);
LogMain.Debug("服务初始化完成");
while (true)
{
@@ -20,21 +28,27 @@ namespace MCSJ
Console.Write("请选择操作: ");
var input = Console.ReadLine();
LogMain.Debug($"用户选择操作: {input}");
switch (input)
{
case "1":
versionManager.DisplayAllVersions();
LogMain.Info("显示所有版本列表");
break;
case "2":
Console.Write("请输入要下载的版本名称: ");
var version = Console.ReadLine();
LogMain.Info($"开始下载版本: {version}");
await downloadService.DownloadVersion(version);
LogMain.Info($"版本下载完成: {version}");
break;
case "3":
LogMain.Info("程序正常退出");
return;
default:
Console.WriteLine("无效输入,请重新选择");
LogMain.Warn($"无效的用户输入: {input}");
break;
}

View 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());
}
}
}

View 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);
}
}