C # Simple Write log

Source: Internet
Author: User

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 {}}///&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.