马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

6秒完成50万条多线程并发日志文件写入【转】

  [复制链接]
查看21348 | 回复22 | 2017-7-27 09:54:26 | 显示全部楼层 |阅读模式
100多行代码实现6秒完成50万条多线程并发日志文件写入,支持日志文件分隔

日志工具类代码:

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

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

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

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

                        Thread.Sleep(1);
                    }
                }
                catch
                {

                }
            }));
            thread.IsBackground = true;
            thread.Start();
        }
        #endregion

        #region 写文件
        /// <summary>
        /// 写文件
        /// </summary>
        public static void WriteFile(List<string> list, string path)
        {
            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }

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

                using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter sw = new 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();
                }
            }
            catch { }
        }
        #endregion

        #region 生成日志文件路径
        /// <summary>
        /// 生成日志文件路径
        /// </summary>
        public static string CreateLogPath()
        {
            int index = 0;
            string logPath;
            bool bl = true;
            do
            {
                index++;
                logPath = Path.Combine(path, "Log" + DateTime.Now.ToString("yyyyMMdd") + (index == 1 ? "" : "_" + index.ToString()) + ".txt");
                if (File.Exists(logPath))
                {
                    FileInfo fileInfo = new FileInfo(logPath);
                    if (fileInfo.Length < fileSize)
                    {
                        bl = false;
                    }
                }
                else
                {
                    bl = false;
                }
            } while (bl);

            return logPath;
        }
        #endregion

        #region 写错误日志
        /// <summary>
        /// 写错误日志
        /// </summary>
        public static void LogError(string log)
        {
            msgQueue.Enqueue(new Tuple<string, string>("[Error] ", log));
        }
        #endregion

        #region 写操作日志
        /// <summary>
        /// 写操作日志
        /// </summary>
        public static void Log(string log)
        {
            msgQueue.Enqueue(new Tuple<string, string>("[Info]  ", log));
        }
        #endregion

    }
}



测试代码:

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            for (int n = 0; n < 10; n++)
            {
                Thread thread = new Thread(new ThreadStart(() =>
                {
                    int i = 0;
                    for (int k = 0; k < 50000; k++)
                    {
                        LogUtil.Log((i++).ToString() + "    abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcda3.1415bcdabcdabcdabcdabc@#$%^&dabcdabcdabcdabcdabcdabcdabcdabcd");
                    }
                }));
                thread.IsBackground = true;
                thread.Start();
            }
        }
    }
}





6秒完成50万条多线程并发日志文件写入,IBC编程社区,www.ibcibc.com,c#教程

6秒完成50万条多线程并发日志文件写入,IBC编程社区,www.ibcibc.com,c#教程





游客,如果您要查看本帖隐藏内容请回复





C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
Amy尾巴 | 2017-7-27 09:58:14 | 显示全部楼层
666
等待时候 | 2017-7-28 08:46:59 | 显示全部楼层
666
tremy | 2017-7-28 11:00:29 | 显示全部楼层
学习一下
neptune88 | 2017-9-28 17:04:42 | 显示全部楼层
楼主感谢分享,下载试下!
neptune88 | 2017-9-28 17:05:06 | 显示全部楼层
楼主感谢分享,下载试下!
菜鸟一只 | 2018-1-24 13:48:15 | 显示全部楼层
学习,学习,学习
214679 | 2018-3-2 00:04:58 | 显示全部楼层
學習一下
a657432983 | 2018-5-10 10:42:20 | 显示全部楼层
iamli01 | 2018-9-19 18:49:04 | 显示全部楼层
不想用log4net的配置文件,参考下大神的代码,多谢
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则