Beispiel #1
0
        private void WriteLogToFile()
        {
            Int64 theCount = 0;

            while (FStop == false)
            {
                try
                {
                    theCount++;
                    string theLogContent = null;
                    //队列操作时需要锁定,否则会报错.队列并不是线程安全的.
                    //但多个队列可以同时写.
                    lock (this)
                    {
                        theLogContent = _logCaches.Dequeue();
                    }
                    if (theLogContent != null && theLogContent != "")
                    {
                        _streamWriter.WriteLine(theLogContent);
                        _streamWriter.Flush();
                        LastExecTime = DateTime.Now;
                    }
                    if (theCount > 10000)
                    {
                        //GC.Collect();
                        theCount = 0;
                    }
                }
                catch (Exception ex)
                {
                    SysAppEventWriter.WriteEvent(-1, ex.Message, System.Diagnostics.EventLogEntryType.Error);
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// 清理30分钟没有发生读写的文件流。对于日志文件按天产生的非常有用。
 /// </summary>
 private void Execute()
 {
     while (FStop == false)
     {
         try
         {
             lock (typeof(LocalLockObject))
             {
                 foreach (var theItem in _Workers)
                 {
                     if (DateTime.Now.Subtract(theItem.Value.LastExecTime).Minutes > Timeout)
                     {
                         theItem.Value.CloseLogThread();
                         _Workers.Remove(theItem.Key);
                     }
                 }
             }
             Thread.Sleep(100000);
         }
         catch (Exception ex)
         {
             SysAppEventWriter.WriteEvent(-1, ex.Message, System.Diagnostics.EventLogEntryType.Error);
         }
     }
 }
Beispiel #3
0
 /// <summary>
 /// 结束日志内置线程,并关闭所有文件流。程序正常退出时调用.
 /// </summary>
 public void CloseAll()
 {
     FStop = true;
     if (_writerThread != null)
     {
         _writerThread.Join();
     }
     try
     {
         lock (typeof(LocalLockObject))
         {
             if (_Workers != null)
             {
                 foreach (var theItem in _Workers)
                 {
                     theItem.Value.CloseLogThread();
                     _Workers.Remove(theItem.Key);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         SysAppEventWriter.WriteEvent(-1, ex.Message, System.Diagnostics.EventLogEntryType.Error);
     }
 }
Beispiel #4
0
 /// <summary>
 /// 结束日志内置线程,并关闭所有文件流。程序正常退出时调用.
 /// </summary>
 public void CloseLogThread()
 {
     FStop = true;
     if (_writerThread != null)
     {
         _writerThread.Join();
     }
     try
     {
         _streamWriter.Flush();
         _streamWriter.Close();
         _streamWriter.Dispose();
     }
     catch (Exception ex)
     {
         SysAppEventWriter.WriteEvent(-1, ex.Message, System.Diagnostics.EventLogEntryType.Error);
     }
 }