异步日志
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -36,21 +37,13 @@ namespace NX_LogUtility
|
|||||||
}
|
}
|
||||||
public static class LogHelper
|
public static class LogHelper
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private readonly static System.Collections.Concurrent.BlockingCollection<string> messages = new System.Collections.Concurrent.BlockingCollection<string>();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region 记录日志为文本文档形式
|
#region 记录日志为文本文档形式
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 记录日志类
|
/// 记录日志类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="Txt">日志内容</param>
|
/// <param name="Txt">日志内容</param>
|
||||||
/// <param name="logType">日志类型(Run:程序运行日志; Err:程序错误日志; DbErr:数据库异常相关日志; OPCRun:OPC运行日志; SocketRun:Socket运行日志; SocErr:Socket异常日志。)</param>
|
/// <param name="logType">日志类型(Run:程序运行日志; Err:程序错误日志; DbErr:数据库异常相关日志; OPCRun:OPC运行日志; SocketRun:Socket运行日志; SocErr:Socket异常日志。)</param>
|
||||||
public static void WriteLog(string Txt, LogTypeEum logType)
|
private static void SaveLog(string Txt, LogTypeEum logType)
|
||||||
{
|
{
|
||||||
//获取日志存放位置,年份不同,存放文件夹不同。
|
//获取日志存放位置,年份不同,存放文件夹不同。
|
||||||
string strDir = System.Environment.CurrentDirectory + "\\NX_CleanMac_Log\\" + DateTime.Now.ToString("yyyy") + "\\";
|
string strDir = System.Environment.CurrentDirectory + "\\NX_CleanMac_Log\\" + DateTime.Now.ToString("yyyy") + "\\";
|
||||||
@@ -69,7 +62,6 @@ namespace NX_LogUtility
|
|||||||
WriteFile(strDir2, filePath, Str);
|
WriteFile(strDir2, filePath, Str);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly object lockobj = new object();
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 日志文件打印类
|
/// 日志文件打印类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -80,15 +72,8 @@ namespace NX_LogUtility
|
|||||||
{
|
{
|
||||||
FileStream fs = null;
|
FileStream fs = null;
|
||||||
StreamWriter sw = null;
|
StreamWriter sw = null;
|
||||||
|
|
||||||
Task.Run(() =>
|
|
||||||
{
|
|
||||||
#if !DEBUG
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#endif
|
|
||||||
lock (lockobj)
|
|
||||||
{
|
|
||||||
if (!Directory.Exists(strDir))//如果日志文件存放的文件夹不存在,就新建一个文件夹。
|
if (!Directory.Exists(strDir))//如果日志文件存放的文件夹不存在,就新建一个文件夹。
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(strDir);
|
Directory.CreateDirectory(strDir);
|
||||||
@@ -102,40 +87,144 @@ namespace NX_LogUtility
|
|||||||
{
|
{
|
||||||
fs = new FileStream(filePath, FileMode.Create);
|
fs = new FileStream(filePath, FileMode.Create);
|
||||||
}
|
}
|
||||||
|
|
||||||
sw = new StreamWriter(fs);
|
sw = new StreamWriter(fs);
|
||||||
//开始写入
|
//开始写入
|
||||||
sw.Write(txt);
|
sw.Write(txt);
|
||||||
|
|
||||||
//清空缓冲区
|
//清空缓冲区
|
||||||
sw.Flush();
|
sw.Flush();
|
||||||
//关闭流
|
//关闭流
|
||||||
sw.Close();
|
sw.Close();
|
||||||
fs.Close();
|
fs.Close();
|
||||||
}
|
}
|
||||||
#if !DEBUG
|
|
||||||
}
|
|
||||||
|
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
#endif
|
|
||||||
if (sw != null)
|
if (sw != null)
|
||||||
sw.Close();
|
sw.Close();
|
||||||
if (fs != null)
|
if (fs != null)
|
||||||
fs.Close();
|
fs.Close();
|
||||||
#if !DEBUG
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#region 异步日志
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加到日志队列
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="content"></param>
|
||||||
|
/// <param name="logType"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool WriteLog(string content, LogTypeEum logType)
|
||||||
|
{
|
||||||
|
RunLogTask();
|
||||||
|
return QueueBlock<QueueMessage>.Add(new QueueMessage() { Content = content, LogType = logType });
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RunLogTask()
|
||||||
|
{
|
||||||
|
|
||||||
|
bool createNew;
|
||||||
|
string threadName = "LogQueue";
|
||||||
|
using (System.Threading.Mutex mtx = new System.Threading.Mutex(true, threadName, out createNew))
|
||||||
|
{
|
||||||
|
if (createNew)
|
||||||
|
{
|
||||||
|
Task.Factory.StartNew(() =>
|
||||||
|
{
|
||||||
|
//从队列中取元素。
|
||||||
|
while (!QueueBlock<QueueMessage>.IsComleted())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var m = QueueBlock<QueueMessage>.Take();
|
||||||
|
SaveLog(m.Content, m.LogType);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("日志线程已经运行");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// BlockingCollection演示消息队列
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
public class QueueBlock<T> where T : class
|
||||||
|
{
|
||||||
|
private static BlockingCollection<T> Colls;
|
||||||
|
public QueueBlock()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
public static bool IsComleted()
|
||||||
|
{
|
||||||
|
if (Colls != null && Colls.IsCompleted)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public static bool HasEle()
|
||||||
|
{
|
||||||
|
if (Colls != null && Colls.Count > 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool Add(T msg)
|
||||||
|
{
|
||||||
|
if (Colls == null)
|
||||||
|
{
|
||||||
|
Colls = new BlockingCollection<T>();
|
||||||
|
}
|
||||||
|
Colls.Add(msg);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T Take()
|
||||||
|
{
|
||||||
|
if (Colls == null)
|
||||||
|
{
|
||||||
|
Colls = new BlockingCollection<T>();
|
||||||
|
}
|
||||||
|
return Colls.Take();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 消息体
|
||||||
|
/// </summary>
|
||||||
|
public class QueueMessage
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 日志内容
|
||||||
|
/// </summary>
|
||||||
|
public string Content { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志类型
|
||||||
|
/// </summary>
|
||||||
|
public LogTypeEum LogType { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion 异步日志
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user