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

推荐订阅源

G
GRAHAM CLULEY
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
W
WeLiveSecurity
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
Project Zero
Project Zero
H
Help Net Security
B
Blog RSS Feed
T
Threatpost
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
博客园 - 【当耐特】
月光博客
月光博客
Google Online Security Blog
Google Online Security Blog
NISL@THU
NISL@THU
The GitHub Blog
The GitHub Blog
P
Privacy & Cybersecurity Law Blog
N
News | PayPal Newsroom
T
Tenable Blog
Simon Willison's Weblog
Simon Willison's Weblog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
小众软件
小众软件
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Jina AI
Jina AI
J
Java Code Geeks
Recent Announcements
Recent Announcements
TaoSecurity Blog
TaoSecurity Blog
MongoDB | Blog
MongoDB | Blog
T
Troy Hunt's Blog
V
Visual Studio Blog
博客园_首页
L
LangChain Blog
SecWiki News
SecWiki News
O
OpenAI News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
A
Arctic Wolf
U
Unit 42

博客园 - 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);
        }