You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

3 years ago
using System;
using System.IO;
namespace EC.Utils
{
public class FileUnitEx
{
/// <summary>
/// 定期清理日志
/// </summary>
/// <param name="dirArr">日志目录</param>
/// <param name="retentionDays">日志保留天数</param>
public static void RegularClearLog(string[] dirArr, int retentionDays)
{
try
{
foreach (string dir in dirArr)
{
string[] files = Directory.GetFiles(dir, "*.txt", SearchOption.AllDirectories);
if (files.Length <= 0) continue;
foreach (string file in files)
{
TimeSpan ts = DateTime.Now.Subtract(new FileInfo(file).LastWriteTime);
if (ts.Days > retentionDays) File.Delete(file);
}
}
}
catch (Exception) { }
}
/// <summary>
/// example: CustomLog("RequestBoxType\\", hexStr);
/// </summary>
/// <param name="subPath"></param>
/// <param name="msg"></param>
public static void CustomLog(string subPath, string msg)
{
var LogMethod = typeof(FileUnit).GetMethod("Log", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
LogMethod.Invoke(null, new object[] { subPath, msg });
}
}
}