Files
CleanMachine/NX_LogUtility/LogHelper.cs

231 lines
7.1 KiB
C#
Raw Normal View History

2021-06-05 14:17:30 +08:00
using System;
2022-01-19 10:31:14 +08:00
using System.Collections.Concurrent;
2021-06-05 14:17:30 +08:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NX_LogUtility
{
public enum LogTypeEum
{
/// <summary>
/// 程序运行日志
/// </summary>
Run,
/// <summary>
/// 程序错误日志
/// </summary>
Err,
/// <summary>
/// 数据库异常相关日志
/// </summary>
DbErr,
/// <summary>
/// OPC运行日志
/// </summary>
OPCRun,
/// <summary>
/// Socket运行日志
/// </summary>
SocketRun,
/// <summary>
/// Socket收发数据日志
/// </summary>
SocketData,
}
public static class LogHelper
{
#region 记录日志为文本文档形式
/// <summary>
/// 记录日志类
/// </summary>
/// <param name="Txt">日志内容</param>
/// <param name="logType">日志类型(Run:程序运行日志; Err:程序错误日志; DbErr:数据库异常相关日志; OPCRun:OPC运行日志; SocketRun:Socket运行日志; SocErr:Socket异常日志。)</param>
2022-01-19 10:31:14 +08:00
private static void SaveLog(string Txt, LogTypeEum logType)
2021-06-05 14:17:30 +08:00
{
2022-01-18 14:42:23 +08:00
//获取日志存放位置,年份不同,存放文件夹不同。
string strDir = System.Environment.CurrentDirectory + "\\NX_CleanMac_Log\\" + DateTime.Now.ToString("yyyy") + "\\";
//获取时间,年月日。
string fileDate = DateTime.Now.ToString("yyyy-MM-dd");
//获取时间,年月。
string fileDate1 = DateTime.Now.ToString("yyyy-MM");
//每一年的不同月份日志放在不同的文件夹,同一月份不同类型的日志放于同一个文件夹。
string strDir1 = strDir + fileDate1 + "\\";
//每一年的相同月份不同日期的日志放在不同的文件夹
string strDir2 = strDir1 + fileDate + "\\";
//不同的日志名称,日志文件不同
string filePath = strDir2 + fileDate + "_" + logType + ".log";
string DateString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
string Str = "发生时间:" + DateString + "|" + Txt + "\r\n";
2022-05-11 15:20:00 +08:00
Console.WriteLine(Str);
2022-01-18 14:42:23 +08:00
WriteFile(strDir2, filePath, Str);
2021-06-05 14:17:30 +08:00
}
/// <summary>
/// 日志文件打印类
/// </summary>
/// <param name="strDir">日志文件存放的文件夹</param>
/// <param name="filePath">日志文件存放路径</param>
/// <param name="txt">日志内容</param>
private static void WriteFile(string strDir, string filePath, string txt)
{
FileStream fs = null;
StreamWriter sw = null;
2022-01-19 10:31:14 +08:00
try
2021-06-05 14:17:30 +08:00
{
2022-01-19 10:31:14 +08:00
if (!Directory.Exists(strDir))//如果日志文件存放的文件夹不存在,就新建一个文件夹。
2021-06-05 14:17:30 +08:00
{
2022-01-19 10:31:14 +08:00
Directory.CreateDirectory(strDir);
}
if (File.Exists(filePath))//日志文件存放路径存在,向日志文件追加记录。
2022-01-18 20:20:10 +08:00
{
2022-01-19 10:31:14 +08:00
fs = new FileStream(filePath, FileMode.Append);
}
else//日志文件存放路径不存在,创建日志文件。
{
fs = new FileStream(filePath, FileMode.Create);
}
sw = new StreamWriter(fs);
//开始写入
sw.Write(txt);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
catch (Exception)
{
}
finally
{
if (sw != null)
sw.Close();
if (fs != null)
fs.Close();
}
}
#endregion
2022-01-18 20:20:10 +08:00
2022-01-19 10:31:14 +08:00
#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()
{
2022-01-19 13:27:34 +08:00
//bool createNew;
//string threadName = "LogQueue";
//using (System.Threading.Mutex mtx = new System.Threading.Mutex(true, threadName, out createNew))
//{
// if (createNew)
// {
Task.Factory.StartNew(() =>
2022-01-19 10:31:14 +08:00
{
2022-01-19 13:27:34 +08:00
//从队列中取元素。
while (!QueueBlock<QueueMessage>.IsComleted())
2022-01-19 10:31:14 +08:00
{
2022-01-19 13:27:34 +08:00
try
2022-01-19 10:31:14 +08:00
{
2022-01-19 13:27:34 +08:00
var m = QueueBlock<QueueMessage>.Take();
SaveLog(m.Content, m.LogType);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
2021-06-05 14:17:30 +08:00
}
2022-01-19 13:27:34 +08:00
});
// }
// else
// {
// Console.WriteLine("日志线程已经运行");
// }
//}
2022-01-19 10:31:14 +08:00
}
2022-01-18 20:20:10 +08:00
2022-01-19 10:31:14 +08:00
/// <summary>
/// BlockingCollection演示消息队列
/// </summary>
/// <typeparam name="T"></typeparam>
public class QueueBlock<T> where T : class
{
2022-01-19 13:27:34 +08:00
private static BlockingCollection<T> Colls = new BlockingCollection<T>();
2022-01-19 10:31:14 +08:00
public QueueBlock()
{
}
public static bool IsComleted()
{
if (Colls != null && Colls.IsCompleted)
2021-06-05 14:17:30 +08:00
{
2022-01-19 10:31:14 +08:00
return true;
2021-06-05 14:17:30 +08:00
}
2022-01-19 10:31:14 +08:00
return false;
}
public static bool HasEle()
{
if (Colls != null && Colls.Count > 0)
2021-06-05 14:17:30 +08:00
{
2022-01-19 10:31:14 +08:00
return true;
2021-06-05 14:17:30 +08:00
}
2022-01-19 10:31:14 +08:00
return false;
}
2021-06-05 14:17:30 +08:00
2022-01-19 10:31:14 +08:00
public static bool Add(T msg)
{
if (Colls == null)
{
Colls = new BlockingCollection<T>();
}
Colls.Add(msg);
return true;
}
2021-06-05 14:17:30 +08:00
2022-01-19 10:31:14 +08:00
public static T Take()
{
if (Colls == null)
{
Colls = new BlockingCollection<T>();
}
return Colls.Take();
}
}
2021-06-05 14:17:30 +08:00
2022-01-19 10:31:14 +08:00
/// <summary>
/// 消息体
/// </summary>
public class QueueMessage
{
/// <summary>
/// 日志内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 日志类型
/// </summary>
public LogTypeEum LogType { get; set; }
2021-06-05 14:17:30 +08:00
}
2022-01-19 10:31:14 +08:00
#endregion 异步日志
2021-06-05 14:17:30 +08:00
}
}