| | |
| | | using Quartz; |
| | | using System; |
| | | using System.IO; |
| | | using System.Threading; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Common.Log; |
| | | |
| | |
| | | [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 |
| | |
| | | { |
| | | 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> |
| | | /// åªæ¸
çLogç®å½ä¸çæä»¶ |
| | | /// </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($"è·³è¿éLogç®å½æä»¶: {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) |
| | | // å°è¯è§£æä¸ºæ¥æï¼æ¯æå¤ç§æ ¼å¼ï¼ |
| | | 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> |
| | | /// æ¸
çç«å°è¯»åä¿¡æ¯è®°å½ç®å½ä¸çææå
容ååç®å½ |
| | | /// å°è¯è§£ææ¥ææä»¶å¤¹åç§° |
| | | /// </summary> |
| | | /// <param name="directoryPath">è¦æ¸
ççç®å½è·¯å¾</param> |
| | | /// <param name="maxAge">æä»¶æå¤§ä¿çæ¶é´</param> |
| | | private static void CleanAllInPlatformInfo(string directoryPath, TimeSpan maxAge) |
| | | /// <param name="folderName">æä»¶å¤¹åç§°</param> |
| | | /// <param name="date">è§£æåºçæ¥æ</param> |
| | | /// <returns>æ¯å¦æåè§£æ</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å¹´MMæddæ¥" // 2025å¹´12æ07æ¥ |
| | | }; |
| | | |
| | | try |
| | | { |
| | | var now = DateTime.Now; |
| | | int deletedCount = 0; |
| | | // å°è¯è§£ææ¥æ |
| | | 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; |
| | | } |
| | | } |
| | | } |