日期:2014-05-18 浏览次数:21005 次
/// <summary> /// 日志对象的缓存队列 /// </summary> private static Queue<Msg> msgsQueue; /// <summary> /// 日志文件保存的路径 /// </summary> private static string path = "Logs\\"; /// <summary> /// 日志写入文件线程的控制标记 /// </summary> private static bool state; /// <summary> /// 日志文件生命周期的时间标记 /// </summary> private static DateTime TimeSign; /// <summary> /// 日志文件写入流对象 /// </summary> private static StreamWriter writer; /// <summary> /// 初始化 /// </summary> public QueueManager() { if (msgsQueue == null) { state = true; if (!Directory.Exists(path)) Directory.CreateDirectory(path); msgsQueue = new Queue<Msg>(); Thread thread = new Thread(work); thread.Start(); } } /// <summary> /// 写入新日志,根据指定的日志对象Msg /// </summary> /// <param name="msg">日志内容对象</param> private void WriteToQueue(Msg msg) { if (msg != null) { lock (msgsQueue) { msgsQueue.Enqueue(msg); } } } /// <summary> /// 日志写入文件线程执行的方法,消费者 /// </summary> private void work() { while (true) { Msg msg = null; //判断队列中是否存在待写入的日志 if (msgsQueue.Count > 0) { lock (msgsQueue) { msg = msgsQueue.Dequeue(); } if (msg != null) { WriteToFile(msg); } } else { //判断是否已经发出终止日志并关闭的消息 if (state) { Thread.Sleep(1); } else { FileClose(); } } } } 。。。。。。。
/// <summary> /// 日志对象的缓存队列 /// </summary> private static Queue<Msg> msgsQueue; /// <summary> /// 日志文件保存的路径 /// </summary> private static string path = "Logs\\"; /// <summary> /// 日志写入文件线程的控制标记,true为正在写入 /// </summary> private static bool state=false; /// <summary> /// 日志文件生命周期的时间标记 /// </summary> private static DateTime TimeSign; /// <summary> /// 日志文件写入流对象 /// </summary> private static StreamWriter writer; private delegate void workDelegate(); private static workDelegate wd;