自己最近写的一组日志处理类(支持高并发处理)
下面的日志处理我在3000个线程同时调用,写20个文件,相当于每个文件150个线程不间断大规模写.没问题,我提高到10000时,出了问题,但不是日志代码的问题,而是系统创建新的线程时报错,建到3500个线程的时候出现问题.后面我会贴出测试代码.
1)FileLogWorker
[csharp]
/// <summary>
/// 文件日志处理类,利用队列机制,让写日志调用和日志写到文件分离,调用
/// 方将要写的日志和目标文件插入到日志队列中去就返回,
/// 然后由内置线程去写到文件里去。这里用了单例模式。
/// </summary>
public class FileLogWorker
{
private Queue<string> _logCaches = null;
private Thread _writerThread = null;
private StreamWriter _streamWriter;
public string FilePath {get;private set;}
public Thread LogWriteThread { get { return _writerThread; } }
public FileLogWorker(string FilePath)
{
this.FilePath = FilePath;
_logCaches = new Queue<string>();
_writerThread = new Thread(new ThreadStart(WriteLogToFile));
FStop = false;
_streamWriter = new StreamWriter(FilePath,true);
_writerThread.Start();
}
private bool FStop = false;
private void WriteLogToFile()
{
Int64 theCount = 0;
while (FStop == false)
{
try
{
theCount++;
string theLogContent = null;
//队列操作时需要锁定,否则会报错.队列并不是线程安全的.
//但多个队列可以同时写.
lock (this)
{
theLogContent = _logCaches.Dequeue();
}
if (theLogContent != null && theLogContent != "")
{
_streamWriter.WriteLine(theLogContent);
_streamWriter.Flush();
LastExecTime = DateTime.Now;
}
if (theCount > 10000)
{
//GC.Collect();
theCount = 0;
}
}
catch (Exception ex)
{
SysAppEventWriter.WriteEvent(-1, ex.Message, System.Diagnostics.EventLogEntryType.Error);
}
}
}
/// <summary>
/// 向文件日志写日志内容
/// </summary>
/// <param name="fileName">日志文件名</param>
/// <param name="logContent">日志内容</param>
public void WriteLogContent(string logContent)
{
lock (this)
{
_logCaches.Enqueue(logContent);
补充:软件开发 , C# ,