string AddEntry(string fmt, object[] args) { CompactEntry entry = null; _lock.EnterWriteLock(); try { if (_table.Count >= MaxEntryCount) { return("Entry 数超过 " + MaxEntryCount + ",没有记入日志"); } DateTime now = DateTime.Now; if (_table.ContainsKey(fmt) == false) { entry = new CompactEntry { Key = fmt, StartTime = now }; _table.Add(fmt, entry); } else { entry = (CompactEntry)_table[fmt]; if (entry.StartTime == DateTime.MinValue) { entry.StartTime = now; } } } finally { _lock.ExitWriteLock(); } lock (entry) { entry.AddData(args); } return(null); }
// 把累积的条目一次性写入日志文件 public int WriteToLog(delegate_writeLog func_writeLog, string style = "") { int count = 0; // List<string> keys = new List<string>(); _lock.EnterReadLock(); try { foreach (string key in _table.Keys) { CompactEntry entry = _table[key]; lock (entry) { count += entry.WriteToLog(func_writeLog, style); } } } finally { _lock.ExitReadLock(); } #if NO _lock.EnterWriteLock(); try { foreach (string key in keys) { _table.Remove(key); } } finally { _lock.ExitWriteLock(); } #endif return(count); }