Simple Log Management Code

Source: Internet
Author: User

Write your own log, the method of deleting the log on a regular basis.

The method is relatively simple, record it.

<summary>///write Log///</summary>//<param name= "STRMSG" > Content </param> <param name= "strpath" > Path (Folder path relative to hycom) </param>//<param name= "FileName" > Log name (Default 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 ("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 Log///</summary>//<param Name= "STRMSG" > Content </param>//<param name= "strpath" > Path (Folder path relative to hycom) </param> public s tatic void Writelog (String strmsg,string strpath) {string fileName = DateTime.Now.ToString ("Yyyy-mm-dd"            ) + ". txt";        Writelog (Strmsg,strpath,filename); }///<summary>///Regular Delete log///</summary>//<param name= "strpath" > Log path < /param> private static void Deletelog (String strpath) {if (Directory.Exists (Strpat h)) {DirectoryInfoDinfor = new DirectoryInfo (strpath); fileinfo[] files = dinfor.                GetFiles (); foreach (FileInfo file in files) {try {//delete create The log date is 3 months before the log if ( (file. Creationtime.addmonths (3), DateTime.Now) <0) {file.                        Delete (); }//delete the last modified log date is 3 months before the log if ( (file. Lastwritetime.addmonths (3), DateTime.Now) <0) {file.                        Delete (); }//delete log name date is 3 months before the log if ( (convert.todatetime (file. Name.substring (0,10)), DateTime.Now.AddMonths ( -3)) <0) {file.                        Delete ();      }} catch (Exception ex) {                  Continue }                }            }        }


Simple Log Management Code

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: 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.