123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using Aitex.Common.Util;
- using Aitex.Core.RT.SCCore;
- using System;
- using System.IO;
- using System.Timers;
- 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 logsDays = 7;
- public int LogsDays
- {
- get { return logsDays < 7 ? 7 : logsDays; }
- set
- {
- logsDays = value;
- }
- }
- public void Initialize()
- {
- deleteTimer = new Timer(1);
- deleteTimer.Elapsed += OnDeleteLog;
- deleteTimer.AutoReset = true;
- deleteTimer.Enabled = true;
- LOG.InnerLogger = this;
- LogsDays = SC.GetValue<int>($"System.LogsSaveDays");
- }
- 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)
- {
- try
- {
- if (deleteTimer.Interval == 1)
- {
- deleteTimer.Interval = 1000 * 60 * 60 * 24;
- }
- string path = string.Format(@"{0}", "Logs");
- FileInfo[] fileInfos;
- DirectoryInfo curFolderInfo = new DirectoryInfo(path);
- fileInfos = curFolderInfo.GetFiles();
- foreach (FileInfo info in fileInfos)
- {
- if (info.Name.Contains("log") && (info.Extension == ".txt" || info.Extension == ".log"))
- {
- DateTime lastWriteTime = DateTime.Parse(info.LastWriteTime.ToShortDateString());
- DateTime intervalTime = DateTime.Now.AddDays(-LogsDays);
- if (lastWriteTime < intervalTime)
- {
- File.Delete(info.FullName);
- }
- }
- }
-
- }
- catch
- {
-
- }
- }
- public void Terminate()
- {
- try
- {
- if (deleteTimer != null)
- {
- deleteTimer.Enabled = false;
- }
- }
- catch (Exception ex)
- {
- System.Diagnostics.Trace.WriteLine(ex.Message);
- }
- }
- }
- }
|