using System.Diagnostics;
using System.IO;
public static class Log
{
//C:\Users\<用户名>\AppData\Local
//private static readonly string LogFilePath = Path.Combine(
// Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
// "MyApp", "Logs", $"log_{DateTime.Now:yyyyMMdd}.txt");
// 指定日志目录为 D:\java\eclipse\dropins,文件名按日期分割
private static readonly string LogFilePath = Path.Combine(
"D:", "java", "eclipse", "dropins", $"log_{DateTime.Now:yyyyMMdd}.txt"); // 示例:D:\java\eclipse\dropins\log_20250327.txt
static Log()
{
var logDir = Path.GetDirectoryName(LogFilePath);
if (!Directory.Exists(logDir))
{
Directory.CreateDirectory(logDir);
}
}
public static void d(string message, LogLevel level = LogLevel.Info)
{
try
{
var logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{level}] {message}{Environment.NewLine}";
File.AppendAllText(LogFilePath, logEntry);
}
catch (Exception ex)
{
// 如果日志记录失败,可以输出到调试窗口
Debug.WriteLine($"日志记录失败: {ex.Message}");
}
}
public enum LogLevel
{
Info,
Warning,
Error
}
}