Simple Log Management Code and simple log management code
You can regularly delete logs by writing your own logs.
The method is relatively simple. record it.
/// <Summary> /// write logs /// </summary> /// <param name = "strMsg"> content </param> /// <param name = "strPath"> path (relative to the folder path under hycom) </param> // <param name = "fileName"> log name (~yyyy-mm-dd.txt) </param> public static void WriteLog (string strMsg, string strPath, string fileName) {string path = AppDomain. currentDomain. baseDirectory + strPath; if (! Path. EndsWith ("\") |! Path. EndsWith ("/") {path + = "\";} if (! Directory. exists (path) {Directory. createDirectory (path);} DeleteLog (path); if (fileName = "") {fileName = DateTime. now. toString ("yyyy-MM-dd") + ". txt ";} if (! FileName. endsWith (". txt ") {fileName + = ". txt ";} try {// string fileName = DateTime. now. toString ("yyyy-MM-dd") + ". txt "; StreamWriter sw = File. appendText (path + fileName); sw. writeLine ("{0 }:{ 1}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss: fff"), strMsg); // sw. writeLine ("\ n \ r"); sw. flush (); sw. close ();} catch {}}/// <summary> /// write logs /// </summary> /// <param name = "strMsg"> content </param> /// <param name = "strPath"> path (relative to the folder path under hycom) </param> public static void WriteLog (string strMsg, string strPath) {string fileName = DateTime. now. toString ("yyyy-MM-dd") + ". txt "; WriteLog (strMsg, strPath, fileName );} /// <summary> /// regularly delete logs /// </summary> /// <param name = "strPath"> Log Path </param> private static void DeleteLog (string strPath) {if (Directory. exists (strPath) {DirectoryInfo dinfor = new DirectoryInfo (strPath); FileInfo [] files = dinfor. getFiles (); foreach (FileInfo file in files) {try {// Delete the log created three months ago if (DateTime. compare (file. creationTime. addMonths (3), DateTime. now) <0) {file. delete ();} // Delete the log that was last modified three months ago if (DateTime. compare (file. lastWriteTime. addMonths (3), DateTime. now) <0) {file. delete ();} // Delete the log name if (DateTime. compare (Convert. toDateTime (file. name. substring (0, 10), DateTime. now. addMonths (-3) <0) {file. delete () ;}} catch (Exception ex) {continue ;}}}}