| | |
| | | using Newtonsoft.Json; |
| | | 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; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Extensions; |
| | | |
| | | namespace WIDESEA_Comm.LogInfo |
| | | { |
| | |
| | | /// </summary> |
| | | public partial class WriteLog |
| | | { |
| | | private int fileSize; |
| | | public static WriteLog log; |
| | | static string EquipName; |
| | | private string logFileName; |
| | | public string FileLogPath { set; get; } |
| | | /// <summary> |
| | | /// åå
¥æ¥å¿(ä¸è¦å¤çº¿ç¨æä½å䏿件ï¼ä¸ä¸ªè®¾å¤ä¸ä¸ªæä»¶å¤¹) |
| | | /// </summary> |
| | | 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)) |
| | | { |
| | |
| | | } |
| | | 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 |
| | | { |
| | |
| | | strwriter.WriteLine(content); |
| | | if (data != null) |
| | | { |
| | | strwriter.WriteLine(JsonConvert.SerializeObject(data)); |
| | | //strwriter.WriteLine(JsonConvert.SerializeObject(data)); |
| | | strwriter.WriteLine(data); |
| | | } |
| | | strwriter.WriteLine("-------------------------------"); |
| | | strwriter.WriteLine(); |
| | |
| | | } |
| | | catch { } |
| | | } |
| | | public static WriteLog Info(string equipName) |
| | | { |
| | | EquipName = equipName; |
| | | //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) |
| | | { |
| | | //åå§å大äº399Mæ¥å¿æä»¶å°èªå¨å é¤; |
| | | |
| | | this.fileSize = 2048 * 1024 * 200;//50M 2048 * 1024 * 200= 419430000åè(b)=399.9996185å
åè(mb) |
| | | |
| | | //é»è®¤è·¯å¾ |
| | | |
| | | //this.FileLogPath = AppContext.BaseDirectory + "\\log\\" + EquipName + "\\"; |
| | | EquipName = equipName; |
| | | if (!string.IsNullOrEmpty(equipName)) |
| | | this.logFileName = equipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; |
| | | else |
| | | this.logFileName = "log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; |
| | | //this.logFileName = EquipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; |
| | | } |
| | | object flag = new object(); |
| | | public void Write(string Message, string equipName) |
| | | { |
| | | lock (flag) |
| | | { |
| | | if (!string.IsNullOrEmpty(equipName)) |
| | | this.logFileName = equipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; |
| | | else |
| | | this.logFileName = "log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; |
| | | this.Write(this.logFileName, Message, equipName); |
| | | } |
| | | } |
| | | public void Write(string LogFileName, string Message, string equipName) |
| | | { |
| | | |
| | | //DirectoryInfo path=new DirectoryInfo(LogFileName); |
| | | //妿æ¥å¿æä»¶ç®å½ä¸åå¨,åå建 |
| | | if (!Directory.Exists(this.FileLogPath)) |
| | | { |
| | | Directory.CreateDirectory(this.FileLogPath); |
| | | } |
| | | |
| | | FileInfo finfo = new FileInfo(this.FileLogPath + LogFileName); |
| | | if (finfo.Exists && finfo.Length > fileSize) |
| | | { |
| | | finfo.Delete(); |
| | | } |
| | | try |
| | | { |
| | | FileStream fs = new FileStream(this.FileLogPath + LogFileName, FileMode.Append); |
| | | StreamWriter strwriter = new StreamWriter(fs); |
| | | try |
| | | { |
| | | |
| | | DateTime d = DateTime.Now; |
| | | strwriter.WriteLine("æ¶é´:" + d.ToString()); |
| | | strwriter.WriteLine(Message); |
| | | strwriter.WriteLine(); |
| | | strwriter.Flush(); |
| | | } |
| | | catch (Exception ee) |
| | | { |
| | | //Console.WriteLine("æ¥å¿æä»¶åå
¥å¤±è´¥ä¿¡æ¯:" + ee.ToString()); |
| | | } |
| | | finally |
| | | { |
| | | strwriter.Close(); |
| | | strwriter = null; |
| | | fs.Close(); |
| | | fs = null; |
| | | } |
| | | } |
| | | catch (Exception ee) |
| | | { |
| | | //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 |
| | | } |
| | | } |