微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

100多行代码实现6秒完成50万条多线程并发日志文件写入

日志工具代码只是测试用,实际项目不用。这里有个自己平时常用的,每秒可以写入1万条日志以上: https://www.cnblogs.com/s0611163/p/4023859.html

100多行代码实现6秒完成50万条多线程并发日志文件写入,支持日志文件分隔

日志工具代码

using System;
 System.Collections.Concurrent;
 System.Collections.Generic;
 System.IO;
 System.Linq;
 System.Text;
 System.Threading;
 System.Threading.Tasks;

namespace Utils
{
    /// <summary>
    /// 写日志类
    </summary>
    public class LogUtil
    {
        #region 字段
        static object _lock = new object();
        string path = "D:\\log";
        int fileSize = 10 * 1024 * 1024; //日志分隔文件大小
        private static ConcurrentQueue<Tuple<string,string>> msgQueue = new ConcurrentQueue<Tuple<string>>#endregion

        #region 静态构造函数
        static LogUtil()
        {
            Thread thread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    int i;
                    List<string> list;
                    Tuple< tuple;

                    while (true)
                    {
                        i = 0;
                        list = new List<();
                        while (msgQueue.TryDequeue(out tuple) && i++ < 10000)
                        {
                            list.Add(tuple.Item1.PadLeft(8) + tuple.Item2);
                        }
                        if (list.Count > )
                        {
                            WriteFile(list,CreateLogPath());
                        }

                        Thread.Sleep(1);
                    }
                }
                catch
                {

                }
            }));
            thread.IsBackground = ;
            thread.Start();
        }
        #region文件
        <summary>
        文件
        </summary>
        void WriteFile(List<string> list,1)">string path)
        {
            if (!Directory.Exists(Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }

                File.Exists(path))
                {
                    using (FileStream fs = new FileStream(path,FileMode.Create)) { fs.Close(); }
                }

                using (StreamWriter sw =  StreamWriter(fs))
                    {
                        list.ForEach(item =>
                        {
                            #region 日志内容
                            string value = string.Format(@"{0} {1}",DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss.fff),item);
                            #endregion

                            sw.WriteLine(value);
                        });

                        sw.Flush();
                    }
                    fs.Close();
                }
            }
             { }
        }
        #region 生成日志文件路径
         生成日志文件路径
         CreateLogPath()
        {
            int index = ;
             logPath;
            bool bl = do
            {
                index++;
                logPath = Path.Combine(path,Log" + DateTime.Now.ToString(yyyyMMdd") + (index == 1 ? "" : _" + index.ToString()) + .txt);
                if (File.Exists(logPath))
                {
                    FileInfo fileInfo =  FileInfo(logPath);
                    if (fileInfo.Length < fileSize)
                    {
                        bl = false;
                    }
                }
                else
                {
                    bl = ;
                }
            } while (bl);

            return logPath;
        }
        #region错误日志
        错误日志
        void LogError( log)
        {
            msgQueue.Enqueue(new Tuple<string>([Error] ,log));
        }
        #region 写操作日志
         写操作日志
        void Log([Info]  

    }
}
View Code

使用条件变量模式改进:


                {
                    Monitor.Enter(_lock);
                    )
                    {
                        Monitor.Wait(_lock);

                        i = =>
            {
                Monitor.Enter(_lock);
                Monitor.pulseAll(_lock);
                Monitor.Exit(_lock);
            });
        }
        

    }
}
View Code

测试代码

 System.ComponentModel;
 System.Data;
 System.Drawing;
 System.Threading.Tasks;
 System.Windows.Forms;
 Utils;

 WindowsFormsApplication1
{
    partial  Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void Form1_Load( sender,EventArgs e)
        {
            LogUtil.path = Application.StartupPath + \\log"; 初始化日志路径
        }

        void button1_Click(for (int n = 0; n < 10; n++)
            {
                Thread thread = int i = ;
                    int k = 0; k < 50000; k++)
                    {
                        LogUtil.Log((i++).ToString() +     abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcda3.1415bcdabcdabcdabcdabc@#$%^&dabcdabcdabcdabcdabcdabcdabcdabcd);
                    }
                }));
                thread.IsBackground = ;
                thread.Start();
            }
        }
    }
}
View Code

 测试截图:

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐