123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- 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<int>("System.LogsSaveDays");
- }
- if (SC.ContainsItem("System.IsEnableCompressLogFile"))
- {
- _isEnableCompressLogFile = SC.GetValue<bool>("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);
- }
- /// <summary>
- /// 定期删除log
- /// </summary>
- /// <returns></returns>
- 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);
- }
- }
- /// <summary>
- /// 压缩文件/文件夹
- /// </summary>
- /// <param name="filePath">需要压缩的文件/文件夹路径</param>
- /// <param name="zipPath">压缩文件路径(zip后缀)</param>
- /// <param name="password">密码</param>
- /// <param name="filterExtenList">需要过滤的文件后缀名</param>
- private bool CompressFile(string filePath, string zipPath, string password = "", List<string> 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;
- }
- /// <summary>
- /// 添加文件夹
- /// </summary>
- /// <param name="zip">ZipFile对象</param>
- /// <param name="dirPath">需要压缩的文件夹路径</param>
- /// <param name="rootPath">根目录路径</param>
- /// <param name="filterExtenList">需要过滤的文件后缀名</param>
- private void AddDirectory(ZipFile zip, string dirPath, string rootPath, List<string> 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);
- }
- }
- }
- }
|