/// <summary> /// 写入日志文件(异步单线程 记录日志) /// </summary> /// <param name="logmede"></param> public static void logWrite(LogModel logmede) { // 这里需要锁上 不然会出现:源数组长度不足。请检查 srcIndex 和长度以及数组的下限。异常 //网上有资料说 http://blog.csdn.net/greatbody/article/details/26135057 不能多线程同时写入队列 //其实 不仅仅 不能同时写入队列 也不能同时读和写如队列 所以 在Dequeue 取的时候也要锁定一个对象 lock (myLock) logQueue.Enqueue(logmede); logStartWrite(); }
/// <summary> /// 开始把队列消息写入文件 /// </summary> private static void logStartWrite() { if (isStart) { return; } isStart = true; Thread t = new Thread(delegate() { while (true) { #region 检测 并删除 之前之外的 日志文件 if (time.AddDays(TestingInterval) <= DateTime.Now)// 时间内 检测一次 { time = DateTime.Now; DirectoryInfo dir = new DirectoryInfo(LogModel._logFilePath); FileHelper.CreatePath(LogModel._logFilePath); var dirs = dir.GetDirectories(); foreach (var dirinfo in dirs) { if (dirinfo.CreationTime.AddDays(DelInterval) <= DateTime.Now)//删除 设定时间 之前的日志 { Directory.Delete(dirinfo.FullName, true); } } } #endregion if (LogHelper.logQueue.Count >= 1) { LogModel m = null; lock (myLock) m = LogHelper.logQueue.Dequeue(); if (m == null) { continue; } if (!Directory.Exists(m.logFilePath + m.logFileName + @"\")) { Directory.CreateDirectory(m.logFilePath + m.logFileName + @"\"); } int i = 0; //部分 日志 文件路径 string SectionfileFullName = m.logFilePath + m.logFileName + @"\" + m.logFileName + "_" + i.ToString("000") + ".txt"; //最新的写了内容的 部分 日志文件路径 string TopSectionfileFullName = SectionfileFullName; // 需要实时更新的 最新日志文件 路径 string LogfileFullNqme = m.logFilePath + m.logFileName + @"\" + m.logFileName + ".txt"; FileInfo file = new FileInfo(SectionfileFullName); while (file.Exists && file.Length >= LogHelper.SectionlogFileSize) { TopSectionfileFullName = SectionfileFullName; i++; SectionfileFullName = m.logFilePath + m.logFileName + @"\" + m.logFileName + "_" + i.ToString("000") + ".txt"; file = new FileInfo(SectionfileFullName); } try { if (!file.Exists)//如果不存在 这个文件 就说明需要 创建新的部分日志文件了 { //因为SectionfileFullName路径的文件不存在 所以创建 File.WriteAllText(SectionfileFullName, m.logMessg, encoding); FileInfo Logfile = new FileInfo(LogfileFullNqme); if (Logfile.Exists && Logfile.Length >= LogHelper.fileSize) { //先清空 然后加上 上一个部分文件的内容 File.WriteAllText(LogfileFullNqme, File.ReadAllText(TopSectionfileFullName, encoding), encoding);//如果存在则覆盖 } } else { File.AppendAllText(SectionfileFullName, m.logMessg, encoding);//累加 } //追加这次内容 到动态更新的日志文件 File.AppendAllText(LogfileFullNqme, m.logMessg, encoding); } catch (Exception ex) { throw ex; } } else { isStart = false; //标记下次可执行 break; //跳出循环 } } }); t.Start(); }