|
@@ -1,21 +1,24 @@
|
|
using LJLib.Tools.File;
|
|
using LJLib.Tools.File;
|
|
using System;
|
|
using System;
|
|
using System.IO;
|
|
using System.IO;
|
|
|
|
+using System.Threading;
|
|
|
|
|
|
namespace LJLib.TextLog
|
|
namespace LJLib.TextLog
|
|
{
|
|
{
|
|
internal class RequestLogger : ILogger
|
|
internal class RequestLogger : ILogger
|
|
{
|
|
{
|
|
private readonly string _logDir;
|
|
private readonly string _logDir;
|
|
|
|
+ private Timer _timer;
|
|
private readonly object _syncRoot = new object();
|
|
private readonly object _syncRoot = new object();
|
|
|
|
|
|
public RequestLogger(string logDir = null)
|
|
public RequestLogger(string logDir = null)
|
|
{
|
|
{
|
|
// 默认目录为执行目录下的 log 文件夹
|
|
// 默认目录为执行目录下的 log 文件夹
|
|
_logDir = logDir ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
|
|
_logDir = logDir ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
|
|
|
|
+ if (!Directory.Exists(_logDir)) Directory.CreateDirectory(_logDir);
|
|
|
|
|
|
- if (!Directory.Exists(_logDir))
|
|
|
|
- Directory.CreateDirectory(_logDir);
|
|
|
|
|
|
+ // 创建定时器:立即启动,每隔24小时执行一次
|
|
|
|
+ _timer = new Timer(CleanupOldLogs, null, 0, 24 * 60 * 60 * 1000);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -47,9 +50,6 @@ namespace LJLib.TextLog
|
|
sw.WriteLine("====================================================================");
|
|
sw.WriteLine("====================================================================");
|
|
sw.WriteLine();
|
|
sw.WriteLine();
|
|
}
|
|
}
|
|
-
|
|
|
|
- // 清理过期日志(7天前的删除)
|
|
|
|
- CleanupOldLogs();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
@@ -61,7 +61,7 @@ namespace LJLib.TextLog
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 清理7天前的日志文件
|
|
/// 清理7天前的日志文件
|
|
/// </summary>
|
|
/// </summary>
|
|
- private void CleanupOldLogs()
|
|
|
|
|
|
+ private void CleanupOldLogs(object state)
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
@@ -88,7 +88,6 @@ namespace LJLib.TextLog
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
- // 忽略清理异常
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|