using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Timers; using Aitex.Core.RT.SCCore; using Ionic.Zip; namespace Aitex.Core.RT.Log { public class LogManager : ICommonLog { public const int MaxLogsMonth = 3; public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("fileAppender"); private static Timer deleteTimer;//定义定时器,定时删除log private int _logsSaveDays; private bool _isEnableCompressLogFile; public void Initialize() { if (SC.ContainsItem("System.LogsSaveDays")) { _logsSaveDays = SC.GetValue("System.LogsSaveDays"); } if (SC.ContainsItem("System.IsEnableCompressLogFile")) { _isEnableCompressLogFile = SC.GetValue("System.IsEnableCompressLogFile"); } deleteTimer = new Timer(1); deleteTimer.Elapsed += OnDeleteLog; deleteTimer.AutoReset = true; deleteTimer.Enabled = true; LOG.InnerLogger = this; } public void Debug(string message) { loginfo.Debug(message); } public void Info(string message) { loginfo.Info(message); } public void Warning(string message) { loginfo.Warn(message); } public void Error(string message) { loginfo.Error(message); } /// /// 定期删除log /// /// void OnDeleteLog(Object source, ElapsedEventArgs e) { if (deleteTimer.Interval == 1) { deleteTimer.Interval = 60 * 1000;//每隔1分钟删除 } else if (deleteTimer.Interval == 60 * 1000) { if (DateTime.Now.Hour != 0 && DateTime.Now.Minute != 10)//下一次是0点10分钟 { return; } else { deleteTimer.Interval = 60 * 60 * 1000; } } else if (deleteTimer.Interval == 60 * 60 * 1000) { if (DateTime.Now.Hour != 0) { return; } } try { string path = string.Format(@"{0}", "Logs"); FileInfo[] fileInfos; DirectoryInfo curFolderInfo = new DirectoryInfo(path); fileInfos = curFolderInfo.GetFiles(); foreach (FileInfo info in fileInfos) { if (info.Extension == ".log" || info.Extension == ".txt" || info.Name.Contains("log")) { DateTime lastWriteTime = DateTime.Parse(info.LastWriteTime.ToShortDateString()); if (_logsSaveDays != 0) { DateTime intervalTime = DateTime.Now.AddDays(-_logsSaveDays); if (lastWriteTime < intervalTime) { File.Delete(info.FullName); } } if (_isEnableCompressLogFile && info.Extension != ".zip" && lastWriteTime < DateTime.Now.AddDays(-1)) { if (CompressFile(info.FullName, $"{curFolderInfo.FullName}//{info.Name}.zip")) { File.Delete(info.FullName); LOG.WriteBackgroundLog(eEvent.INFO_WINRESOURCE, "System", $"delete log successfully,logName:{info.Name}"); } } } } } catch(Exception ex) { LOG.WriteExeption(ex); } } /// /// 压缩文件/文件夹 /// /// 需要压缩的文件/文件夹路径 /// 压缩文件路径(zip后缀) /// 密码 /// 需要过滤的文件后缀名 private bool CompressFile(string filePath, string zipPath, string password = "", List filterExtenList = null) { try { using (ZipFile zip = new ZipFile(Encoding.UTF8)) { if (!string.IsNullOrWhiteSpace(password)) { zip.Password = password; } if (Directory.Exists(filePath)) { if (filterExtenList == null) zip.AddDirectory(filePath); else AddDirectory(zip, filePath, filePath, filterExtenList); } else if (File.Exists(filePath)) { zip.AddFile(filePath, ""); } zip.Save(zipPath); return true; } } catch (Exception ex) { LOG.WriteExeption(ex); } return false; } /// /// 添加文件夹 /// /// ZipFile对象 /// 需要压缩的文件夹路径 /// 根目录路径 /// 需要过滤的文件后缀名 private void AddDirectory(ZipFile zip, string dirPath, string rootPath, List filterExtenList) { var files = Directory.GetFiles(dirPath); for (int i = 0; i < files.Length; i++) { if (filterExtenList == null || (filterExtenList != null && !filterExtenList.Any(d => Path.GetExtension(files[i]).ToLower() == d.ToLower()))) { string relativePath = Path.GetFullPath(dirPath).Replace(Path.GetFullPath(rootPath), ""); zip.AddFile(files[i], relativePath); } } var dirs = Directory.GetDirectories(dirPath); for (int i = 0; i < dirs.Length; i++) { AddDirectory(zip, dirs[i], rootPath, filterExtenList); } } public void Terminate() { try { if (deleteTimer != null) { deleteTimer.Enabled = false; } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } } } }