From 5da3a276b7847187a7c155ee069d3cd4c9e58074 Mon Sep 17 00:00:00 2001
From: HuBingJie <3146306518@qq.com>
Date: 星期日, 07 十二月 2025 23:28:14 +0800
Subject: [PATCH] 1

---
 代码管理/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs |  228 +++++++++++++++++---------------------------------------
 1 files changed, 71 insertions(+), 157 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
index 684b1e5..62cf614 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
@@ -1,7 +1,6 @@
 锘縰sing Quartz;
 using System;
 using System.IO;
-using System.Threading;
 using System.Threading.Tasks;
 using WIDESEA_Common.Log;
 
@@ -10,10 +9,6 @@
     [DisallowConcurrentExecution]
     public class LogJob : IJob
     {
-        // 瀹氫箟鍏佽鍒犻櫎鐨勬牴鐩綍
-        private static readonly string LogRootPath = Path.GetFullPath(Environment.CurrentDirectory + "/Log");
-        private static readonly string PlatformInfoPath = Path.GetFullPath(Environment.CurrentDirectory + "/Log/绔欏彴璇诲彇淇℃伅璁板綍");
-
         public Task Execute(IJobExecutionContext context)
         {
             try
@@ -22,195 +17,114 @@
                 {
                     try
                     {
-                            // 娓呯悊鏃ュ織鏂囦欢
-                            CleanLogFilesOnly(LogRootPath, TimeSpan.FromMinutes(43200));
-                            CleanAllInPlatformInfo(PlatformInfoPath, TimeSpan.FromMinutes(43200));
+                        WriteLog.GetLog("LogJob").Write($"鏃ュ織璁板綍锛歿DateTime.Now}", "LogJob");
 
-                            Thread.Sleep(1000 * 10); // 10绉掑悗鍐嶆鎵ц
+                        // 璁剧疆淇濈暀澶╂暟涓�30澶�
+                        int saveDays = 30;
+
+                        // 娓呯悊Log鐩綍涓嬬殑鎵�鏈夎繃鏈熸枃浠跺す
+                        CleanLogDirectory(saveDays);
+
+                        Thread.Sleep(1000 * 10);
                     }
                     catch (Exception ex)
                     {
-                        Console.WriteLine($"鏃ュ織浠诲姟鎵ц寮傚父: {ex.Message}");
+                        WriteLog.GetLog("LogJob").Write($"鏃ュ織娓呯悊鍑洪敊锛歿ex.Message}", "LogJob");
                     }
                 });
             }
             catch (Exception ex)
             {
-                Console.WriteLine($"{nameof(LogJob)} 鍒濆鍖栧紓甯�: {ex.Message}");
+                Console.Out.WriteLine(nameof(LogJob) + ":" + ex.Message);
             }
 
             return Task.CompletedTask;
         }
 
         /// <summary>
-        /// 妫�鏌ヨ矾寰勬槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
+        /// 娓呯悊Log鐩綍涓嬬殑鎵�鏈夎繃鏈熸棩蹇楁枃浠跺す
         /// </summary>
-        /// <param name="path">瑕佹鏌ョ殑璺緞</param>
-        /// <param name="allowedRoot">鍏佽鐨勬牴鐩綍</param>
-        /// <returns>鏄惁鍏佽鍒犻櫎</returns>
-        private static bool IsPathAllowed(string path, string allowedRoot)
+        /// <param name="saveDays">淇濈暀澶╂暟</param>
+        private static void CleanLogDirectory(int saveDays)
         {
-            try
-            {
-                var fullPath = Path.GetFullPath(path);
-                return fullPath.StartsWith(allowedRoot, StringComparison.OrdinalIgnoreCase);
-            }
-            catch
-            {
-                return false;
-            }
-        }
+            // 鑾峰彇Log鐩綍璺緞
+            var logBasePath = Path.Combine(Environment.CurrentDirectory, "Log");
 
-        /// <summary>
-        /// 鍙竻鐞哃og鐩綍涓嬬殑鏂囦欢
-        /// </summary>
-        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
-        /// <param name="maxAge">鏂囦欢鏈�澶т繚鐣欐椂闂�</param>
-        private static void CleanLogFilesOnly(string directoryPath, TimeSpan maxAge)
-        {
-            if (!Directory.Exists(directoryPath))
+            if (!Directory.Exists(logBasePath))
             {
-                //Console.WriteLine($"鐩綍涓嶅瓨鍦�: {directoryPath}");
+                WriteLog.GetLog("LogJob").Write($"Log鐩綍涓嶅瓨鍦細{logBasePath}", "LogJob");
                 return;
             }
 
-            try
+            var nowTime = DateTime.Now;
+            var cutoffDate = nowTime.AddDays(-saveDays);
+
+            WriteLog.GetLog("LogJob").Write($"寮�濮嬫竻鐞嗘棩蹇楋紝淇濈暀澶╂暟锛歿saveDays}锛屾埅姝㈡棩鏈燂細{cutoffDate:yyyy-MM-dd}", "LogJob");
+
+            // 鑾峰彇Log鐩綍涓嬬殑鎵�鏈夊瓙鐩綍
+            var allSubDirs = Directory.GetDirectories(logBasePath, "*", SearchOption.AllDirectories);
+
+            foreach (var dirPath in allSubDirs)
             {
-                var now = DateTime.Now;
-                // 鍙幏鍙栧綋鍓嶇洰褰曠殑鏂囦欢锛屼笉鍖呭惈瀛愮洰褰�
-                var files = Directory.GetFiles(directoryPath, "*", SearchOption.TopDirectoryOnly);
-                int deletedCount = 0;
-
-                foreach (var file in files)
+                try
                 {
-                    try
-                    {
-                        // 妫�鏌ユ枃浠舵槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
-                        if (!IsPathAllowed(file, LogRootPath))
-                        {
-                            Console.WriteLine($"璺宠繃闈濴og鐩綍鏂囦欢: {file}");
-                            continue;
-                        }
+                    // 鑾峰彇鏂囦欢澶瑰悕
+                    var dirName = Path.GetFileName(dirPath);
 
-                        // 鑾峰彇鏂囦欢鍒涘缓鏃堕棿
-                        var creationTime = File.GetCreationTime(file);
-
-                        // 妫�鏌ユ枃浠舵槸鍚﹁秴杩囨寚瀹氭椂闂�
-                        if (now - creationTime > maxAge)
-                        {
-                            File.Delete(file);
-                            deletedCount++;
-                            //Console.WriteLine($"宸插垹闄ゆ枃浠�: {file}");
-                        }
-                    }
-                    catch (Exception ex)
+                    // 灏濊瘯瑙f瀽涓烘棩鏈燂紙鏀寔澶氱鏍煎紡锛�
+                    if (TryParseDateFolder(dirName, out DateTime folderDate))
                     {
-                        Console.WriteLine($"鍒犻櫎鏂囦欢 {file} 鏃跺嚭閿�: {ex.Message}");
+                        // 鍒ゆ柇鏄惁杩囨湡
+                        if (folderDate < cutoffDate)
+                        {
+                            WriteLog.GetLog("LogJob").Write($"鍒犻櫎杩囨湡鏃ュ織鏂囦欢澶癸細{dirPath}锛屾棩鏈燂細{folderDate:yyyy-MM-dd}", "LogJob");
+
+                            // 閫掑綊鍒犻櫎鏂囦欢澶瑰強鍏跺唴瀹�
+                            Directory.Delete(dirPath, true);
+                        }
+                        else
+                        {
+                            WriteLog.GetLog("LogJob").Write($"淇濈暀鏃ュ織鏂囦欢澶癸細{dirPath}锛屾棩鏈燂細{folderDate:yyyy-MM-dd}", "LogJob");
+                        }
                     }
                 }
+                catch (Exception ex)
+                {
+                    WriteLog.GetLog("LogJob").Write($"澶勭悊鏂囦欢澶� {dirPath} 鏃跺嚭閿欙細{ex.Message}", "LogJob");
+                }
+            }
 
-                //Console.WriteLine($"鐩綍 {directoryPath} 娓呯悊瀹屾垚锛屽垹闄や簡 {deletedCount} 涓枃浠�");
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"娓呯悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
-            }
+            WriteLog.GetLog("LogJob").Write($"鏃ュ織娓呯悊瀹屾垚", "LogJob");
         }
 
         /// <summary>
-        /// 娓呯悊绔欏彴璇诲彇淇℃伅璁板綍鐩綍涓嬬殑鎵�鏈夊唴瀹瑰拰瀛愮洰褰�
+        /// 灏濊瘯瑙f瀽鏃ユ湡鏂囦欢澶瑰悕绉�
         /// </summary>
-        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
-        /// <param name="maxAge">鏂囦欢鏈�澶т繚鐣欐椂闂�</param>
-        private static void CleanAllInPlatformInfo(string directoryPath, TimeSpan maxAge)
+        /// <param name="folderName">鏂囦欢澶瑰悕绉�</param>
+        /// <param name="date">瑙f瀽鍑虹殑鏃ユ湡</param>
+        /// <returns>鏄惁鎴愬姛瑙f瀽</returns>
+        private static bool TryParseDateFolder(string folderName, out DateTime date)
         {
-            if (!Directory.Exists(directoryPath))
+            // 鏀寔鐨勬棩鏈熸牸寮�
+            string[] dateFormats =
             {
-                //Console.WriteLine($"鐩綍涓嶅瓨鍦�: {directoryPath}");
-                return;
-            }
+                "yyyy-MM-dd",   // 2025-12-07
+                "yyyyMMdd",     // 20251207
+                "yyyy_MM_dd",   // 2025_12_07
+                "yyyy.MM.dd",   // 2025.12.07
+                "yyyy骞碝M鏈坉d鏃�" // 2025骞�12鏈�07鏃�
+            };
 
-            try
-            {
-                var now = DateTime.Now;
-                int deletedCount = 0;
+            // 灏濊瘯瑙f瀽鏃ユ湡
+            bool success = DateTime.TryParseExact(
+                folderName,
+                dateFormats,
+                System.Globalization.CultureInfo.InvariantCulture,
+                System.Globalization.DateTimeStyles.None,
+                out date
+            );
 
-                // 鍒犻櫎鎵�鏈夎繃鏈熺殑鏂囦欢
-                var files = Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories);
-                foreach (var file in files)
-                {
-                    try
-                    {
-                        // 妫�鏌ユ枃浠舵槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
-                        if (!IsPathAllowed(file, PlatformInfoPath))
-                        {
-                            Console.WriteLine($"璺宠繃闈炵珯鍙颁俊鎭洰褰曟枃浠�: {file}");
-                            continue;
-                        }
-
-                        var creationTime = File.GetCreationTime(file);
-                        if (now - creationTime > maxAge)
-                        {
-                            File.Delete(file);
-                            deletedCount++;
-                            //Console.WriteLine($"宸插垹闄ゆ枃浠�: {file}");
-                        }
-                    }
-                    catch (Exception ex)
-                    {
-                        Console.WriteLine($"鍒犻櫎鏂囦欢 {file} 鏃跺嚭閿�: {ex.Message}");
-                    }
-                }
-
-                // 鍒犻櫎鎵�鏈夌┖鐩綍锛堝寘鎷瓙鐩綍锛�
-                DeleteAllEmptySubdirectories(directoryPath);
-
-                //Console.WriteLine($"鐩綍 {directoryPath} 娓呯悊瀹屾垚锛屽垹闄や簡 {deletedCount} 涓枃浠�");
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"娓呯悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
-            }
-        }
-
-        /// <summary>
-        /// 閫掑綊鍒犻櫎鎵�鏈夌┖瀛愮洰褰�
-        /// </summary>
-        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
-        private static void DeleteAllEmptySubdirectories(string directoryPath)
-        {
-            if (!Directory.Exists(directoryPath))
-                return;
-
-            try
-            {
-                // 閫掑綊澶勭悊鎵�鏈夊瓙鐩綍
-                foreach (var subDirectory in Directory.GetDirectories(directoryPath))
-                {
-                    // 妫�鏌ョ洰褰曟槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
-                    if (!IsPathAllowed(subDirectory, PlatformInfoPath))
-                    {
-                        Console.WriteLine($"璺宠繃闈炵珯鍙颁俊鎭洰褰�: {subDirectory}");
-                        continue;
-                    }
-
-                    DeleteAllEmptySubdirectories(subDirectory);
-
-                    // 妫�鏌ュ瓙鐩綍鏄惁涓虹┖
-                    var files = Directory.GetFiles(subDirectory);
-                    var directories = Directory.GetDirectories(subDirectory);
-
-                    if (files.Length == 0 && directories.Length == 0)
-                    {
-                        Directory.Delete(subDirectory);
-                        //Console.WriteLine($"宸插垹闄ょ┖鐩綍: {subDirectory}");
-                    }
-                }
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"澶勭悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
-            }
+            return success;
         }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3