using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WinFormsAppVisitorDeploy { /// /// 日志记录帮助类 /// public class MyLogHelper { /// /// 读取配置信息是否记录调试信息:1、不记录;其他、记录 /// public static string MyLogDebug = System.Configuration.ConfigurationManager.AppSettings["MyLogDebug"] + ""; /// /// 正常消息队列 /// private static ConcurrentQueue infoMsgQueue = new ConcurrentQueue(); /// /// 调试消息队列 /// private static ConcurrentQueue debugMsgQueue = new ConcurrentQueue(); /// /// 错误消息队列 /// private static ConcurrentQueue errorMsgQueue = new ConcurrentQueue(); /// /// 控制文件写入线程 /// private static Task logTask; /// /// 记录信息 /// /// public static void WriteMsg(LogInfoMo infoObj) { try { if (infoObj != null) { switch (infoObj.msgType) { case EnumLogMsgTypeEnum.Info: infoMsgQueue.Enqueue(infoObj); break; case EnumLogMsgTypeEnum.Debug: if (MyLogDebug == "1") { //不记录调试信息 } else { debugMsgQueue.Enqueue(infoObj); } break; case EnumLogMsgTypeEnum.Error: errorMsgQueue.Enqueue(infoObj); break; } WriteExecute(); } } catch (Exception ex) { infoObj.message = "队列异常:" + ex; errorMsgQueue.Enqueue(infoObj); } } /// /// 执行文件写入 /// private static void WriteExecute() { try { //判断线程是否创建 if (logTask == null) { logTask = Task.Factory.StartNew(async () => { while (true) { //判断是否退出线程 if (MyTaskControlHelper.TokenSource.IsCancellationRequested) { return; } // 初始化为true时执行WaitOne不阻塞 MyTaskControlHelper.ResetEvent.WaitOne(); // 模拟等待1ms await Task.Delay(1); //正常信息写入文件 try { if (infoMsgQueue.TryDequeue(out LogInfoMo infoObj)) { WriteFire(infoObj); } } catch (Exception ex) { string str = "普通信息队列处理异常:" + ex; errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); } //错误信息写入文件 try { if (errorMsgQueue.TryDequeue(out LogInfoMo infoObj)) { WriteFire(infoObj); } } catch (Exception ex) { string str = "错误信息队列处理异常:" + ex; errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); } //调试信息写入文件 try { if (debugMsgQueue.TryDequeue(out LogInfoMo infoObj)) { WriteFire(infoObj); } } catch (Exception ex) { string str = "调试信息队列处理异常:" + ex; errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); } } }); } } catch (Exception ex) { string str = "文件队列处理异常:" + ex; errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); } } /// /// 将信息写入文件 /// /// /// private static void WriteFire(LogInfoMo infoObj) { try { //获取程序所在目录 string pathStr = AppDomain.CurrentDomain.BaseDirectory + "log\\" + (string.IsNullOrEmpty(infoObj.path) ? "" : infoObj.path + "\\") + DateTime.Now.Year + "\\" + DateTime.Now.ToString("yyyyMM") + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\"; //判断文件夹是否存在 if (Directory.Exists(pathStr) == false) { //不存在文件夹则创建 Directory.CreateDirectory(pathStr); } string firePath = pathStr + infoObj.msgType.ToString() + DateTime.Now.ToString("yyyyMMddHH") + ".txt"; using (FileStream fs = new FileStream(firePath, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { string str = DateTime.Now + "\t" + infoObj.message; sw.WriteLine(str); } } } catch (Exception ex) { string str = DateTime.Now + "\t 文件写入异常:" + ex; errorMsgQueue.Enqueue(new LogInfoMo() { message = str, msgType = EnumLogMsgTypeEnum.Error }); } } } /// /// 日志信息类型枚举 /// public enum EnumLogMsgTypeEnum { /// /// 正常信息 /// Info = 0, /// /// 错误信息 /// Error = 1, /// /// 调试信息 /// Debug = 2, } /// /// 操作日志记录类型 /// public enum EnumOperationLogType { //0、查询;1、新增;2、修改;3、删除;4、异常信息;5、其他 /// /// 查询 /// Query = 0, /// /// 新增 /// Add = 1, /// /// 更新 /// Update = 2, /// /// 删除 /// Delete = 3, /// /// 异常 /// Error = 4, /// /// 其他 /// Other = 5, } /// /// 记录信息数据模型 /// public class LogInfoMo { /// /// 信息保存路径,默认不单独保存 /// public string path { get; set; } /// /// 记录信息内容字符串 /// public string message { get; set; } /// /// 信息类型 /// public EnumLogMsgTypeEnum msgType { get; set; } } }