博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
100多行代码实现6秒完成50万条多线程并发日志文件写入
阅读量:6942 次
发布时间:2019-06-27

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

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

日志工具类代码:

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{    ///     /// 写日志类    ///     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
> msgQueue = new ConcurrentQueue
>(); #endregion #region 静态构造函数 static LogUtil() { Thread thread = new Thread(new ThreadStart(() => { try { int i; List
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 > 0) { WriteFile(list, CreateLogPath()); } Thread.Sleep(1); } } catch { } })); thread.IsBackground = true; thread.Start(); } #endregion #region 写文件 ///
/// 写文件 /// public static void WriteFile(List
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 生成日志文件路径 ///
/// 生成日志文件路径 /// 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 写错误日志 ///
/// 写错误日志 /// public static void LogError(string log) { msgQueue.Enqueue(new Tuple
("[Error] ", log)); } #endregion #region 写操作日志 ///
/// 写操作日志 /// public static void Log(string log) { msgQueue.Enqueue(new Tuple
("[Info] ", log)); } #endregion }}
View Code

使用条件变量模式改进:

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{    ///     /// 写日志类    ///     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
> msgQueue = new ConcurrentQueue
>(); #endregion #region 静态构造函数 static LogUtil() { Thread thread = new Thread(new ThreadStart(() => { try { Monitor.Enter(_lock); int i; List
list; Tuple
tuple; while (true) { Monitor.Wait(_lock); i = 0; list = new List
(); while (msgQueue.TryDequeue(out tuple) && i++ < 10000) { list.Add(tuple.Item1.PadLeft(8) + tuple.Item2); } if (list.Count > 0) { WriteFile(list, CreateLogPath()); } } } catch { } })); thread.IsBackground = true; thread.Start(); } #endregion #region 写文件 ///
/// 写文件 /// public static void WriteFile(List
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 生成日志文件路径 ///
/// 生成日志文件路径 /// 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 写错误日志 ///
/// 写错误日志 /// public static void LogError(string log) { msgQueue.Enqueue(new Tuple
("[Error] ", log)); Task.Factory.StartNew(() => { Monitor.Enter(_lock); Monitor.PulseAll(_lock); Monitor.Exit(_lock); }); } #endregion #region 写操作日志 ///
/// 写操作日志 /// public static void Log(string log) { msgQueue.Enqueue(new Tuple
("[Info] ", log)); Task.Factory.StartNew(() => { Monitor.Enter(_lock); Monitor.PulseAll(_lock); Monitor.Exit(_lock); }); } #endregion }}
View Code

测试代码:

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();            }        }    }}
View Code

 测试截图:

 

转载于:https://www.cnblogs.com/s0611163/p/7234361.html

你可能感兴趣的文章
JAVA - JAVA编译运行过程
查看>>
Android 手势识别类 ( 三 ) GestureDetector 源码浅析
查看>>
numpy.percentile
查看>>
[maven] 使用Nexus创建maven私有仓库
查看>>
Linux下安装JDK
查看>>
java设计模式之策略
查看>>
解决Centos关闭You have new mail in /var/spool/mail/root提示
查看>>
手把手教你反编译别人的APP
查看>>
MapReduce的集群行为和框架
查看>>
Oracle表变化趋势追踪记录
查看>>
排序算法总结之希尔排序
查看>>
python中set使用
查看>>
gradle项目与maven项目相互转化(转)
查看>>
MAC下安装与配置MySQL
查看>>
linux系统的crond服务
查看>>
Restore Volume 操作 - 每天5分钟玩转 OpenStack(60)
查看>>
sqool导出oracle数据
查看>>
MyBatis动态传入表名,字段名参数的解决办法
查看>>
Windows平台下安装Hadoop
查看>>
oracle11gR2静默安装
查看>>