博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Queue 队列 写日志简单应用
阅读量:4960 次
发布时间:2019-06-12

本文共 1713 字,大约阅读时间需要 5 分钟。

public class LogContent

    {
        public LogContent()
        {
           
        }
        public LogContent(string msg)
        {
            this.Message = msg;
        }
        private string mssage;
        public string Message
        {
            get;
            set;
        }
    }

public static class FileQueueManager

    {
        private static object lockHelper = new object();
        static Thread workerThread;
        static FileQueueManager()
        {
            workerThread = new Thread(new ThreadStart(Run));
            workerThread.Start();
        }
        private static Queue<LogContent> logMessageQueue = new Queue<LogContent>();
        public static void Write(LogContent log)
        {
            lock (lockHelper)
            {
                logMessageQueue.Enqueue(log);
            }
        }
        public static int QueueCount
        {
            get { return logMessageQueue.Count; }
        }
        private static void Run()
        {
            lock (lockHelper)
            {
                Write(new LogContent() { Message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") });
            }
            LogContent log;
            while (true)
            {
                try
                {
                    if (logMessageQueue.Count > 0)
                    {
                        Write();
                        lock (lockHelper)
                        {
                            logMessageQueue.Dequeue();
                        }
                    }
                    else
                        break;
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                }
            }
        }
        private static void Write()
        {
            string logdir;
            //logdir = System.Web.HttpContext.Current.Server.MapPath(logFileName);
            logdir = @"d:\a.log";
            try
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(logdir, true);
                sw.BaseStream.Seek(0, System.IO.SeekOrigin.End);
                sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + DateTime.Now.Millisecond + "]" + workerThread.ManagedThreadId + "");
                sw.Flush();
                sw.Close();
            }
            catch (Exception e)
            {
            }
        }
    }

private static void ThreadQueueTest()

        {
            for (int i = 0; i < 100; i++)
                FileQueueManager.Write(new LogContent() { Message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") });
            System.Console.WriteLine(FileQueueManager.QueueCount);
        }

转载于:https://www.cnblogs.com/sk-net/archive/2011/10/14/2212280.html

你可能感兴趣的文章
组件:slot插槽
查看>>
走进C++程序世界------异常处理
查看>>
Nginx配置文件nginx.conf中文详解(转)
查看>>
POJ 1988 Cube Stacking
查看>>
POJ 1308 Is It A Tree?(并查集)
查看>>
N进制到M进制的转换问题
查看>>
Android------三种监听OnTouchListener、OnLongClickListener同时实现即其中返回值true或者false的含义...
查看>>
MATLAB实现多元线性回归预测
查看>>
Mac xcode 配置OpenGL
查看>>
利用sed把一行的文本文件改成每句一行
查看>>
使用Asyncio的Coroutine来实现一个有限状态机
查看>>
Android应用开发:核心技术解析与最佳实践pdf
查看>>
python——爬虫
查看>>
2.2 标识符
查看>>
孤荷凌寒自学python第五十八天成功使用python来连接上远端MongoDb数据库
查看>>
求一个字符串中最长回文子串的长度(承接上一个题目)
查看>>
简单权限管理系统原理浅析
查看>>
springIOC第一个课堂案例的实现
查看>>
求输入成绩的平均分
查看>>
php PDO (转载)
查看>>