Self-organized a simple write log class, convenient for later use again.
Description: CS Program, program startup directory, automatically create log directory, write logs. Logs are recorded by day, and daily logs are saved as a TXT file.
<summary>///////////////////////</summary> public class Logmanager {Priv Ate static string logdirectory = Application.startuppath + @ "\log\"; <summary>////The maximum number of logs saved///</summary> private static int maxlogcount = 365; public static void Write (String msg) {try {deletelog (); if (! System.IO.Directory.Exists (logdirectory)) {System.IO.Directory.CreateDirectory (Logdirec Tory); } String name = DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString (). PadLeft (2, ' 0 ') + DateTime.Now.Day.ToString (). PadLeft (2, ' 0 '); String datapath = logdirectory + name + ". txt"; if (System.IO.File.Exists (datapath))//Append {using (FileStream fs = new FileStream (datapat H, Filemode.append)) { using (StreamWriter SW = new StreamWriter (FS)) {//start writing Sw. WriteLine ("\ n"); Sw. WriteLine ("Current time:" + DateTime.Now.ToString ()); Sw. WriteLine ("Log message:" + msg); Clears the buffer sw. Flush (); Close the stream SW. Close (); Fs. Close (); }}} else//create {using (FileStream fs = New FileStream (DataPath, FileMode.Create)) {using (StreamWriter SW = new Strea Mwriter (FS)) {//start writing SW. WriteLine ("Current time:" + DateTime.Now.ToString ()); Sw. WriteLine ("Log message:" + msg); emptying buffers Sw. Flush (); Close the stream SW. Close (); Fs. Close (); }}}} catch {}} public STA tic void Write (Exception ex) {try {deletelog (); if (! System.IO.Directory.Exists (logdirectory)) {System.IO.Directory.CreateDirectory (Logdirec Tory); } String name = DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString (). PadLeft (2, ' 0 ') + DateTime.Now.Day.ToString (). PadLeft (2, ' 0 '); String datapath = logdirectory + name + ". txt";//File format: 20170606.txt if (System.IO.File.Exists (datapath))// Append {using (FileStream fs = new FileStream (DataPath, Filemode.append)) {using (StreamWriter SW = New StreamWriter (FS)) {//begins writing to SW. WriteLine ("\ n"); Sw. WriteLine ("Current time:" + DateTime.Now.ToString ()); Sw. WriteLine ("Exception info:" + ex.) Message); Sw. WriteLine ("Exception object:" + ex.) Source); Sw. WriteLine ("Call stack:" + ex.) StackTrace); Sw. WriteLine ("Trigger method:" + ex.) TargetSite); Clears the buffer sw. Flush (); Close the stream SW. Close (); Fs. Close (); }}} else//create {using (FileStream fs = New FileStream (DataPath, FileMode.Create)) {using (StreamWriter SW = new Strea Mwriter (FS)) {//start writing SW. WriteLine ("Current time: "+ DateTime.Now.ToString ()); Sw. WriteLine ("Exception info:" + ex.) Message); Sw. WriteLine ("Exception object:" + ex.) Source); Sw. WriteLine ("Call stack:" + ex.) StackTrace); Sw. WriteLine ("Trigger method:" + ex.) TargetSite); Clears the buffer sw. Flush (); Close the stream SW. Close (); Fs. Close (); }}}} ' catch {}}///<SU mmary>///delete Log//</summary> private static void Deletelog () {Directory Info dic = new DirectoryInfo (logdirectory); Don't have to delete the logs every day, remove the IF (!) 10 days. ( DateTime.Now.Day% = = 0)) {return; } if (dic. Exists) {var files = dic. GetFiles (); if (Files. Length < Maxlogcount) return; list<int> names = new list<int> (); for (int i = 0; i < files. Length; i++) {String strname = Path.getfilenamewithoutextension (Files[i]. Name); int name; if (int. TryParse (strname, out name)) {names. ADD (name); }} names. Sort ();//Ascending//names. Reverse ();//descending for (int i = 0; i < names. Count-maxlogcount; i++) {String path = System.IO.Path.Combine (Logdirectory, Names[i]. ToString () + ". txt"); File.delete (path); } } } }
C # Simple Write log