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

IT技术2年前 (2022)发布 投稿用户
0

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

© 版权声明
好牛新坐标 广告
版权声明:
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com

相关文章