添加LogToFile静态变量控制日志是否输出到文件
This commit is contained in:
@@ -348,6 +348,11 @@ namespace JoyD.Windows.CS.Toprie
|
||||
|
||||
#region 私有方法
|
||||
|
||||
/// <summary>
|
||||
/// 控制是否将日志输出到文件的标志
|
||||
/// </summary>
|
||||
public static bool LogToFile = true; // 默认开启文件日志输出
|
||||
|
||||
/// <summary>
|
||||
/// 记录日志到控制台和文件
|
||||
/// </summary>
|
||||
@@ -356,18 +361,20 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 输出到控制台
|
||||
Console.WriteLine(message);
|
||||
|
||||
// 输出到日志文件
|
||||
try
|
||||
// 输出到日志文件(仅当日志到文件功能开启时)
|
||||
if (LogToFile)
|
||||
{
|
||||
lock (_logLock)
|
||||
try
|
||||
{
|
||||
string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log.txt");
|
||||
// 确保目录存在
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(logPath));
|
||||
// 写入日志,包含时间戳
|
||||
File.AppendAllText(logPath, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}\r\n");
|
||||
lock (_logLock)
|
||||
{
|
||||
string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log.txt");
|
||||
// 确保目录存在
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(logPath));
|
||||
// 写入日志,包含时间戳
|
||||
File.AppendAllText(logPath, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果写入日志文件失败,只输出到控制台
|
||||
|
||||
Reference in New Issue
Block a user