/// <summary> /// 记录日志信息 /// </summary> /// <param name="level">日志级别</param> /// <param name="srcName">日志源</param> /// <param name="logDescripton">日志描述</param> public static void OnLogEvent(LogTextLevel level, string srcName, string logDescripton) { // 没有使能日志, 快速返回 if (!_isLogging) { return; } //去掉空格和数字 // string functionName = Regex.Replace(srcName, @"[\s<\d>]", string.Empty); DateTime NowTime = DateTime.Now; // 判断级别,是否将该条日志记录到日志文件中 if ((int)LogLevel <= (int)level) { MemLog NewLog = new MemLog(); NewLog.LogTime = NowTime; NewLog.LogLevel = level; NewLog.LogSrcName = srcName; NewLog.LogDesc = logDescripton; //添加重复日志的过滤 _logMem.Enqueue(NewLog); // 激活写日志文件线程 _logSaveWait.Set(); } }
/// <summary> /// 内存日志最大条数上线检测 /// </summary> //private static void CheckOverMaxLogCount() //{ // if (!(_logMem.Count > 0)) // { // return; // } // if (_logMem.Count > maxLogCount) // { // //一次清空500条 // while (_logMem.Count > (maxLogCount-500)) // { // //在首减少日志 // _logMem.RemoveAt(0); // if (_logSaveIndex > 0) // { // _logSaveIndex--; // } // else // { // _logSaveIndex = _logMem.Count; // } // } // } //} /// <summary> /// 日志文件写线程,没有可写的日志文件时会自己挂起 /// </summary> private static void WriteThread() { try { while (true) { cts.Token.ThrowIfCancellationRequested(); // 当前线程在日志队列写完后自动挂起 if (_logMem.IsEmpty && !cts.IsCancellationRequested) { _logSaveWait.WaitOne(); } else { try { MemLog LogToSave = null; if (_logMem.TryDequeue(out LogToSave)) { FileInfo fileInfo = new FileInfo(_logPath); // 没有日志文件 if (!string.IsNullOrEmpty(_logPath) && IsPrintToFile) { using (StreamWriter logFile = File.AppendText(_logPath)) { logFile.Write(LogToSave.LogTime.ToString("[yyyy-MM-dd HH:mm:ss.fff]")); logFile.Write("[" + LogToSave.LogLevel.ToString() + "]"); logFile.Write("[" + LogToSave.LogSrcName + "]"); logFile.Write(LogToSave.LogDesc + "\r\n"); logFile.Close(); } } //// 显示到输出框 if (IsPrintToOutput) { Console.WriteLine("[{0}]<{1}>{2}", LogToSave.LogLevel.ToString(), LogToSave.LogSrcName, LogToSave.LogDesc); } } } catch (Exception e) { Console.WriteLine("LogText.WriteThread() 失败 : {0}", e.Message); } } } } catch (OperationCanceledException) { Console.WriteLine("日志线程正常退出"); } }
private Task <int> LoadAsync() { return(Task.Run <int>(() => { int icount = 0; using (StreamReader reader = new StreamReader(vm.LogFileName, Encoding.UTF8)) { IList <MemLog> temps = new List <MemLog>(); while (!reader.EndOfStream) { string text = reader.ReadLine(); MemLog log = new MemLog(); Match matchResult = Regex.Match(text, "\\[([^\\]]+)\\]\\[([^\\]]+)\\]\\[([^\\]]+)\\](.*)"); if (matchResult.Success) { if (matchResult.Groups.Count == 5) { log.LogTime = matchResult.Groups[1].Value; log.LogLevel = (LogTextLevel)Enum.Parse(typeof(LogTextLevel), matchResult.Groups[2].Value); log.LogSrcName = matchResult.Groups[3].Value; log.LogDesc = matchResult.Groups[4].Value; if (vm.FilterDate.HasValue) { if (DateTime.Parse(log.LogTime) >= new DateTime(2017, 11, 7)) { temps.Add(log); icount++; } } else { temps.Add(log); icount++; } } else { Console.WriteLine("异常数据:" + text); } } } App.Current.Dispatcher.Invoke(() => { vm.Logs = new Comm.ViewableCollection <MemLog>(temps); }); } return icount; })); }
// -------------------------------------------------------------- public Status(string name) { log = new MemLog(name, 5); }
// -------------------------------------------------------------- public Status(string name) { log = new MemLog(name, cfg.s.StatusLogDepth); }
protected virtual void OnMemLog(MemLogEventArgs e) { MemLog?.Invoke(this, e); }