// 把累积的条目一次性写入日志文件 public void WriteToLog(delegate_writeLog func_writeLog) { List <string> keys = new List <string>(); _lock.EnterReadLock(); try { foreach (string key in _table.Keys) { CompactEntry entry = _table[key]; lock (entry) { entry.WriteToLog(func_writeLog); } } } finally { _lock.ExitReadLock(); } _lock.EnterWriteLock(); try { foreach (string key in keys) { _table.Remove(key); } } finally { _lock.ExitWriteLock(); } }
public void WriteToLog(delegate_writeLog func_writeLog, string style = "display") { if (this.Datas.Count == 0) { return; } if (style == "display") { // 适合观看的格式 StringBuilder text = new StringBuilder(); text.Append("(" + this.TotalCount + " 项) 压缩日志 "); text.Append(this.Key + "\r\n"); int i = 0; foreach (CompactData data in this.Datas) { text.Append((i + 1).ToString() + ") " + (new DateTime(this.StartTime.Ticks + data.Ticks)).ToString("HH:mm:ss") + ":"); text.Append(string.Format(this.Key, data.Args) + "\r\n"); i++; } if (i < this.TotalCount) { text.Append("... (余下 " + (this.TotalCount - i) + " 项被略去)"); } func_writeLog(text.ToString()); } else { // 原始格式 StringBuilder text = new StringBuilder(); text.Append("(" + this.TotalCount + " 项)"); text.Append(this.Key + "\r\n"); int i = 0; foreach (CompactData data in this.Datas) { text.Append((i + 1).ToString() + ") " + data.GetString(this.StartTime) + "\r\n"); i++; } if (i < this.TotalCount) { text.Append("... (余下 " + (this.TotalCount - i) + " 项被略去)"); } func_writeLog(text.ToString()); } this.Datas.Clear(); // 写入日志后,清除内存 this.TotalCount = 0; }
public static void Start( List <ProcessInfo> process_infos, delegate_writeLog writeLog, CancellationToken token) { // 首次等待 TimeSpan wait_time = TimeSpan.FromSeconds(30); Task.Run(async() => { while (token.IsCancellationRequested == false) { foreach (ProcessInfo info in process_infos) { if (token.IsCancellationRequested) { return; } if (HasModuleStarted(info.MutexName) == true) { continue; } // TODO: 写入日志 writeLog?.Invoke(info, "进程被重新启动"); // 启动 StartModule(info.ShortcutPath, "minimize"); } // 延时 try { await Task.Delay(// TimeSpan.FromMilliseconds(1000), wait_time, token); } catch { return; } } }); }
// 把累积的条目一次性写入日志文件 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); }
public int WriteToLog(delegate_writeLog func_writeLog, string style = "") { if (this.Datas.Count == 0) { // 2021/3/28 if (StringUtil.IsInList("reset_start_time", style)) { this.StartTime = DateTime.MinValue; } return(0); } string entry_time_format = StringUtil.GetParameterByPrefix(style, "entry_time_format"); string data_format = StringUtil.GetParameterByPrefix(style, "data_format"); if (string.IsNullOrEmpty(entry_time_format)) { entry_time_format = "HH:mm:ss"; } if (style == "display" || string.IsNullOrEmpty(data_format) || data_format == "display") { // 适合观看的格式 StringBuilder text = new StringBuilder(); text.Append("(" + this.TotalCount + " 项) 压缩日志 "); text.Append(this.Key + "\r\n"); int i = 0; foreach (CompactData data in this.Datas) { text.Append((i + 1).ToString() + ") " + data.GetTimeString(this.StartTime, entry_time_format) + ":"); text.Append(string.Format(this.Key, data.Args) + "\r\n"); i++; } if (i < this.TotalCount) { text.Append("... (余下 " + (this.TotalCount - i) + " 项被略去)"); } func_writeLog(text.ToString()); } else { // 原始格式 StringBuilder text = new StringBuilder(); text.Append("(" + this.TotalCount + " 项)"); text.Append(this.Key + "\r\n"); int i = 0; foreach (CompactData data in this.Datas) { // 逐项显示 data.Args List <string> args = new List <string>(); foreach (var arg in data.Args) { args.Add(arg == null ? "(null)" : arg.ToString()); } text.Append((i + 1).ToString() + ") " + data.GetString(this.StartTime, entry_time_format) + " " + StringUtil.MakePathList(args, ",") + "\r\n"); i++; } if (i < this.TotalCount) { text.Append("... (余下 " + (this.TotalCount - i) + " 项被略去)"); } func_writeLog(text.ToString()); } int count = (int)this.TotalCount; this.Datas.Clear(); // 写入日志后,清除内存 this.TotalCount = 0; // 2021/3/28 if (StringUtil.IsInList("reset_start_time", style)) { this.StartTime = DateTime.MinValue; } return(count); }