惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
Cyberwarzone
Cyberwarzone
S
Securelist
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
T
Tenable Blog
NISL@THU
NISL@THU
博客园 - 三生石上(FineUI控件)
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
C
CXSECURITY Database RSS Feed - CXSecurity.com
G
Google Developers Blog
Forbes - Security
Forbes - Security
月光博客
月光博客
博客园 - 叶小钗
Spread Privacy
Spread Privacy
Last Week in AI
Last Week in AI
H
Help Net Security
TaoSecurity Blog
TaoSecurity Blog
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
N
News and Events Feed by Topic
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Hacker News
The Hacker News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Privacy International News Feed
D
DataBreaches.Net
O
OpenAI News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Latest news
Latest news
J
Java Code Geeks
Project Zero
Project Zero
V
V2EX
Security Latest
Security Latest
AI
AI

博客园 - s80895304

win8 framework3.5安装 ChangeCharToTable sql 触发器 mysql 备忘sql ActiveMQ 安装 【转】j2ee 环境搭建 C#原始类型扩展方法—this参数修饰符 消息队列(Message Queue)简介及其使用 IE中同一个url第二次AJAX调用无法触发onreadystatechange事件 Web Service 身份验证 sql 索引 Memcached、MongoDB、Redis和tokyotyrant 【转】ActiveMQ redis iphone 技巧 10款iOS高效开发必备的Objective-C类库 ASIHTTPRequest jquery 倒计时插件 js keycode
Queue 队列 写日志简单应用
s80895304 · 2011-10-14 · via 博客园 - s80895304

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);
        }