FALogFileCleaner.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.IO;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.Util;
  5. namespace Aitex.RT.FactoryAutomation
  6. {
  7. class FALogFileCleaner
  8. {
  9. PeriodicJob _threadDeleteLogs;
  10. public void Run()
  11. {
  12. //1天运行一次,删除一个月之前的FA log文件
  13. _threadDeleteLogs = new PeriodicJob(1000 * 60 * 60 * 24, OnDeleteLog, "DeleteFALog Thread", true);
  14. }
  15. bool OnDeleteLog()
  16. {
  17. try
  18. {
  19. string path = string.Format(@"{0}", "Logs");
  20. FileInfo[] fileInfos;
  21. DirectoryInfo curFolderInfo = new DirectoryInfo(path);
  22. fileInfos = curFolderInfo.GetFiles();
  23. foreach (FileInfo info in fileInfos)
  24. {
  25. if (info.Name.Contains("FabConnect") && info.Extension == ".log")
  26. {
  27. DateTime lastWriteTime = DateTime.Parse(info.LastWriteTime.ToShortDateString());
  28. DateTime intervalTime = DateTime.Now.AddMonths(-1);// DateTime.Parse(DateTime.Now.AddMonths(-1).ToShortDateString());
  29. if (lastWriteTime < intervalTime)
  30. {
  31. File.Delete(info.FullName);
  32. //LOG.Write(string.Format("【FA】自动删除FA log成功,logName:{0}", info.Name));
  33. }
  34. }
  35. }
  36. }
  37. catch (Exception ex)
  38. {
  39. LOG.Write(ex);
  40. }
  41. return true;
  42. }
  43. public void Stop()
  44. {
  45. _threadDeleteLogs.Stop();
  46. }
  47. }
  48. }