C # program Singleton log output,

Source: Internet
Author: User

C # program Singleton log output,

For a complete program system, a log record is essential. You can use it to record the running status and error information of a program during running. For example, errors that do not want to be prompted through the dialog box, exceptions caught during program execution, etc.

First, create a new class in the appropriate directory in your solution, such as LogManager:

Write the following code:

1 /// <summary> 2 /// log management 3 /// </summary> 4 public class LogManager 5 {6 private string _ logDir; // log file storage directory 7 8 private static LogManager m_LogInstance; // static singleton object 9 // static constructor 10 static LogManager () 11 {12 m_LogInstance = new LogManager (); 13} 14 // private constructor (required function, which cannot be instantiated externally) 15 private LogManager () 16 {17 _ logDir = Environment. currentDirectory + "\ Log"; 18 this. delOldFile (); 19} 20 // <summary> 21 // /Attribute get singleton object 22 // </summary> 23 public static LogManager LogInstance24 {25 get {return m_LogInstance ;} 26} 27 28 /// <summary> 29 // write a log record 30 /// </summary> 31 /// <param name = "pLog"> log record content </param> 32 public void WriteLog (string pLog) 33 {34 lock (this. _ logDir) // exclusive lock: prevents multiple threads in the main program from simultaneously accessing the same file. Error 35 {36 // create a log file 37 var vDT = DateTime according to the time. now; 38 string vLogFile = string. format ("{0} \ Log {1} {2} {3 }. log ", _ logD Ir, vDT. year, vDT. month, vDT. day); 39 // create a file stream for writing 40 using (FileStream fs = new FileStream (vLogFile, FileMode. append) 41 {42 StreamWriter sw = new StreamWriter (fs); 43 sw. writeLine ("{0 }>>{ 1}", vDT. toString ("yyyy-MM-dd HH: mm: ss"), pLog); 44 sw. flush (); 45 sw. close (); 46 fs. close (); 47} 48} 49} 50 51 // Delete the expired file 52 private void DelOldFile () 53 {54 // traverse all the sub-files in the specified folder, delete the log files before a certain period of time. 55 if (! Directory. exists (this. _ logDir) 56 {57 // If the folder Directory does not exist 58 Directory. createDirectory (this. _ logDir); 59 return; 60} 61 62 var vFiles = (new DirectoryInfo (this. _ logDir )). getFiles (); 63 for (int I = vFiles. length-1; I> = 0; I --) 64 {65 // specify the condition, and then delete 66 if (vFiles [I]. name. contains ("Log") 67 {68 if (DateTime. now-vFiles [I]. lastWriteTime ). days> 7) 69 {70 vFiles [I]. delete (); 71} 72} 73} 74} 75 76} // end class

Among them, rows 8th-26th are a way to implement the singleton mode. In this way, execute at the location where your program needs:

LogManager. LogInstance. WriteLog ("A log record is generated ");

It will record the parameters in the specified file one day.

That is, 23:53:45> A log record is generated.

 

Related Article

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.