志在指尖
用双手敲打未来

几秒秒完成50万条多线程并发日志文件写入

usingSystem;
usingSystem.Collections.Concurrent;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
namespaceUtils
{
///
///写日志类
///
publicclassLogUtil
{
#region字段
publicstaticobject_lock=newobject();
publicstaticstringpath=”D:\\log”;
publicstaticintfileSize=10*1024*1024;//日志分隔文件巨细
privatestaticConcurrentQueue>msgQueue=newConcurrentQueue>();
#endregion
#region静态结构函数
staticLogUtil()
{
Threadthread=newThread(newThreadStart(()=>
{
try
{
inti;
Listlist;
Tupletuple;
while(true)
{
i=0;
list=newList();
while(msgQueue.TryDequeue(outtuple)&&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写文件
///
///写文件
///
publicstaticvoidWriteFile(Listlist,stringpath)
{
try
{
if(!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
if(!File.Exists(path))
{
using(FileStreamfs=newFileStream(path,FileMode.Create)){fs.Close();}
}
using(FileStreamfs=newFileStream(path,FileMode.Append,FileAccess.Write))
{
using(StreamWritersw=newStreamWriter(fs))
{
list.ForEach(item=>
{
#region日志内容
stringvalue=string.Format(@”{0}{1}”,DateTime.Now.ToString(“yyyy-MM-ddHH:mm:ss.fff”),item);
#endregion
sw.WriteLine(value);
});
sw.Flush();
}
fs.Close();
}
}
catch{}
}
#endregion
#region生成日志文件途径
///
///生成日志文件途径
///
publicstaticstringCreateLogPath()
{
intindex=0;
stringlogPath;
boolbl=true;
do
{
index++;
logPath=Path.Combine(path,”Log”+DateTime.Now.ToString(“yyyyMMdd”)+(index==1?””:”_”+index.ToString())+”.txt”);
if(File.Exists(logPath))
{
FileInfofileInfo=newFileInfo(logPath);
if(fileInfo.Length<fileSize)
{
bl=false;
}
}
else
{
bl=false;
}
}while(bl);
returnlogPath;
}
#endregion
#region写过错日志
///
///写过错日志
///
publicstaticvoidLogError(stringlog)
{
msgQueue.Enqueue(newTuple(“[Error]”,log));
}
#endregion
#region写操作日志
///
///写操作日志
///
publicstaticvoidLog(stringlog)
{
msgQueue.Enqueue(newTuple(“[Info]”,log));
}
#endregion
}
}
测试代码:
[C#]纯文本检查仿制代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
usingUtils;
namespaceWindowsFormsApplication1
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
LogUtil.path=Application.StartupPath+”\\log”;//初始化日志途径
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
for(intn=0;n<10;n++)
{
Threadthread=newThread(newThreadStart(()=>
{
inti=0;
for(intk=0;k<50000;k++)
{
LogUtil.Log((i++).ToString()+”abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcda3.1415bcdabcdabcdabcdabc@#$%^&dabcdabcdabcdabcdabcdabcdabcdabcd”);
}
}));
thread.IsBackground=true;
thread.Start();
}
}
}
}

未经允许不得转载:IT技术网站 » 几秒秒完成50万条多线程并发日志文件写入
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载