From 1e38f44af86775a341e4d0e4411d24ad35e417d3 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期五, 28 六月 2024 07:49:36 +0800 Subject: [PATCH] 调整平面图调用接口时间 --- 代码管理/PCS/WCS_Server/WIDESEA_Comm/LogInfo/WriteLog.cs | 87 ++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 85 insertions(+), 2 deletions(-) diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/PCS/WCS_Server/WIDESEA_Comm/LogInfo/WriteLog.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/PCS/WCS_Server/WIDESEA_Comm/LogInfo/WriteLog.cs" index 4295f6c..e269dba 100644 --- "a/\344\273\243\347\240\201\347\256\241\347\220\206/PCS/WCS_Server/WIDESEA_Comm/LogInfo/WriteLog.cs" +++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/PCS/WCS_Server/WIDESEA_Comm/LogInfo/WriteLog.cs" @@ -2,6 +2,7 @@ using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net.Http.Json; using System.Reflection.Metadata; @@ -27,6 +28,13 @@ public static void Write_Log(string groupName, string logName, string content, object data = null) { string basePath = System.Environment.CurrentDirectory + "/Log/" + $"/{groupName}/{DateTime.Now.ToString("yyyy_MM_dd")}"; + + #region 鍒犻櫎鏂囦欢澶瑰唴鎸囧畾鏃ユ湡鍓嶆枃浠跺す + string name = Environment.CurrentDirectory + "/Log/" + $"/{groupName}"; + if (Directory.Exists(name)) + DeleteFoldersOlderThan(name, DateTime.Now.AddDays(-30)); + #endregion + //濡傛灉鏃ュ織鏂囦欢鐩綍涓嶅瓨鍦�,鍒欏垱寤� if (!Directory.Exists(basePath)) { @@ -34,7 +42,7 @@ } try { - FileStream fs = new FileStream(basePath + "/" + logName + $"{DateTime.Now.ToString("yyyy_MM_dd")}_Log.txt", FileMode.Append); + FileStream fs = new FileStream(basePath + "/" + logName + $"_{DateTime.Now.ToString("yyyy_MM_dd")}_Log.txt", FileMode.Append); StreamWriter strwriter = new StreamWriter(fs); try { @@ -43,7 +51,8 @@ strwriter.WriteLine(content); if (data != null) { - strwriter.WriteLine(JsonConvert.SerializeObject(data)); + //strwriter.WriteLine(JsonConvert.SerializeObject(data)); + strwriter.WriteLine(data); } strwriter.WriteLine("-------------------------------"); strwriter.WriteLine(); @@ -64,6 +73,13 @@ //if (log == null) log = new WriteLog("Info" + equipName); log.FileLogPath = AppContext.BaseDirectory + "\\log\\Info\\" + DateTime.Now.ToString("yyyyMMdd") /*+ "\\" + EquipName + "_" + DateTime.Now.ToString("yyyyMMdd")*/ + "\\"; + + #region 鍒犻櫎鏂囦欢澶瑰唴鎸囧畾鏃ユ湡鍓嶆枃浠跺す + string name = AppContext.BaseDirectory + "\\log\\Info\\"; + if (Directory.Exists(name)) + DeleteFoldersOlderThan(name, DateTime.Now.AddDays(-30)); + #endregion + return log; } private WriteLog(string equipName) @@ -139,5 +155,72 @@ //Console.WriteLine("鏃ュ織鏂囦欢娌℃湁鎵撳紑,璇︾粏淇℃伅濡備笅:"); } } + + #region 鑷姩娓呴櫎鏃ュ織 + + /// <summary> + /// 鍒犻櫎鏂囦欢澶瑰唴鎸囧畾鏃ユ湡鍓嶆枃浠跺す + /// </summary> + /// <param name="folderPath"></param> + /// <param name="cutOffDate"></param> + public static void DeleteFoldersOlderThan(string folderPath, DateTime cutOffDate) + { + DirectoryInfo di = new DirectoryInfo(folderPath); + foreach (DirectoryInfo subDir in di.GetDirectories()) + { + // 鑾峰彇鏂囦欢澶圭殑鏈�鍚庤闂椂闂� + DateTime lastAccess = subDir.CreationTime; + //DateTime lastAccess = subDir.LastAccessTime; + + // 濡傛灉鏂囦欢澶圭殑鏈�鍚庤闂椂闂存棭浜庢垨绛変簬鎴鏃ユ湡锛屽垯鍒犻櫎璇ユ枃浠跺す + if (lastAccess <= cutOffDate) + { + try + { + Directory.Delete(subDir.FullName, true); // 閫掑綊鍒犻櫎鏂囦欢澶瑰強鍏跺唴瀹� + //Console.WriteLine($"鏂囦欢 {subDir.FullName} 宸插垹闄ゃ��"); + } + catch (Exception ex) + { + //Console.WriteLine($"鏃犳硶鍒犻櫎鏂囦欢 {subDir.FullName}: {ex.Message}"); + } + } + } + } + + /// <summary> + /// 鍒犻櫎鏂囦欢澶瑰唴鎸囧畾鏃ユ湡鍓嶆枃浠� + /// </summary> + /// <param name="directoryPath"></param> + /// <param name="cutOffDate"></param> + public static void DeleteFilesOlderThan(string directoryPath, DateTime cutOffDate) + { + DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath); + + // 鑾峰彇鎵�鏈夋枃浠跺苟绛涢�夊嚭鍒涘缓鏃ユ湡鍦ㄦ寚瀹氭棩鏈熶箣鍓嶇殑鏂囦欢 + FileInfo[] filesToDelete = directoryInfo.GetFiles() + .Where(f => f.CreationTime < cutOffDate).ToArray(); + + foreach (FileInfo file in filesToDelete) + { + try + { + file.Delete(); // 鍒犻櫎鏂囦欢 + Console.WriteLine($"鏂囦欢 {file.FullName} 宸插垹闄ゃ��"); + } + catch (Exception ex) + { + Console.WriteLine($"鏃犳硶鍒犻櫎鏂囦欢 {file.FullName}: {ex.Message}"); + } + } + } + + //public static void Main() + //{ + // string path = @"C:\YourDirectoryPath"; // 鏇挎崲涓轰綘鐨勭洰褰曡矾寰� + // DateTime cutOff = new DateTime(2023, 3, 31); // 璁剧疆鍒犻櫎鏂囦欢鐨勬埅姝㈡棩鏈� + // DeleteFilesOlderThan(path, cutOff); + //} + #endregion } } -- Gitblit v1.9.3