A simple Log class

Often you need to log recorded in some places when you run the program, prior to want to use Log4Net, but the feeling you want to attach a dll, exe file you want with only a run, simply write their own simple class of.

Conditions need to be met: multiple threads calling; accuracy of the time is not important to know the relationship has on the line; do not want to block the thread, after all, work is the main purpose, in order to log the time and do not care about blocking wait a little worthwhile.

Then, starting with: 1 requires static class, stored in the current default directory, you can specify the name of the log file; 2 requires a method used to write to the file.

public static class LogWriter
{
   static string logPath ="Log_"+ DateTime.Now.ToString("yyyyMMdd") + ".txt";    
   private static void WriteLine_Helper(string str)
        {
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logPath, true))
            {
                writer.WriteLine(DateTime.Now.ToString("yyyy/MM/dd  HH:mm:ss    ") + str);
                writer.Close();
            }
        }
}

Then, write directly, then there will be problems simultaneous access, without the use of locks blocking, where two List stored message queues, and with a bool variable UseList1 to identify the store to List Currently, when a storage List1 time, to create a thread list2 contents written to the log messages and clear list2, then negated UseList1, list2 stored message to the queue, then the message is written to the log List1 and clears the message list1, final inspection list1 and list2 whether there is a message, if there continues to be written.

static List < String > List1 = new new List < String > ();
 static List < String > List2 = new new List < String > ();
 static  BOOL UseList1 = to false ;
 static  BOOL isUsed = to false ; 

public  static  void the WriteLine ( String STR)      // the message into a List 
        {
             IF (UseList1)     // If you are in the List1 write log, it will put messages List2 
            { 
                list2.Add (STR); 
            } 
            the else 
            { 
                list1.Add (STR); 
            } 

            IF (! isUsed)     // if not open thread, the thread turn 
            { 
                isUsed = to true ; 
                the Thread T = new new the Thread (writelist); 
                t.start (); 
            } 
        } 
Private  static  void writelist ()    // the list of messages written to log 
        {
             IF (UseList1)    // If you are using list1, then the message is written to the list1 log 
            { 
                the write (list1); 
                List1.Clear ();   // list1 write completion message after clearing message list1 
                UseList1 = to false ;    // negated UseList1, after the message is written to the List2 
                the Write (list2);    // the message in list2 write log 
                list2.Clear ();    // Clear message in list2 
            }
             the else 
            { 
                the write (list2); 
                list2.Clear (); 
                UseList1 = to true ; 
                the write (List1); 
                List1.Clear (); 
            } 
            isClear ();   // ) check the write is there a new message into the log period, if you continue to write
            = isUsed to false ; 
        } 

Private  static  void the Write (List < String > list)    // the list of messages written to the log file 
        {
             for ( int I = 0 ; I <list.Count; I ++ ) 
            { 
                WriteLine_Helper (list [I] ); 
            } 
        } 

public  static  void isClear ()    // check whether list1 and list2 whether there is not written message 
        {
             iF (list1.Count> 0 || list2.Count> 0 
            {
                WriteList();
            }
        }

Practice found that when high-frequency write log log message does lag a few seconds, should be frequently build thread and frequent opening and closing files caused. Can create a thread waiting for a message, add a list count, when the list of messages in a certain amount of time and then open the log file is written, reducing the number of files open and operating, but only occasionally look at the log for state use, do not toss . . .

Guess you like

Origin www.cnblogs.com/Elvis-Luo/p/11470538.html