当前位置: 首页 > news >正文

NtLogPath

public class NtLogPath { public string LogPath { get; private set; } public string curfilepath = string.Empty; public string errorLgFile = string.Empty; //定义从Exception到Fault这5个层级为Error public string currentDate = DateTime.Now.ToString("yyyy-MM-dd"); public static string CreateLogDirectory() { string path = AppDomain.CurrentDomain.BaseDirectory; //在Windows Service中运行时会出错 // 获取当前应用程序域的名称(通常是程序集名称) string assemblyName = AppDomain.CurrentDomain.FriendlyName; // 去掉路径和扩展名,只保留文件名 assemblyName = Path.GetFileNameWithoutExtension(assemblyName); // 组合成完整的路径 path = System.IO.Path.Combine(path, assemblyName + "Log"); //TempLog.logging(path); //创建日志文件夹 Directory.CreateDirectory(path); return path; } public void CheckAndRotateLogFile() { string today = DateTime.Now.ToString("yyyy-MM-dd"); if (today != currentDate) { currentDate = today; this.MakeLogDirectFileName(); } } public void MakeLogDirectFileName() { // 获取当前应用程序域的名称(通常是程序集名称) string assemblyName = AppDomain.CurrentDomain.FriendlyName; // 去掉路径和扩展名,只保留文件名 assemblyName = Path.GetFileNameWithoutExtension(assemblyName); string path = AppDomain.CurrentDomain.BaseDirectory; // 组合成完整的路径 path = System.IO.Path.Combine(path, assemblyName + "Log"); //创建日志文件夹 Directory.CreateDirectory(path); string dn = DateTime.Now.ToString("yyyy-MM-dd"); this.curfilepath = Path.Combine(this.LogPath, assemblyName + dn + ".log"); this.errorLgFile = Path.Combine(this.LogPath, assemblyName + dn + "err.log"); } } public class NtLogV5 { private BlockingCollection<LogContentV4> logQueue = new BlockingCollection<LogContentV4>(); public NtLogStatistic logStatistic =new NtLogStatistic(); public NtLogPath logPath = new NtLogPath(); private Task task; //不要删除,多线程写日志时会用到 private volatile bool IsWriting = false; public bool Writing { get { return IsWriting; } } public NtLogV5() { this.logPath.MakeLogDirectFileName(); } public void Start() { if (IsWriting) return; IsWriting = true; StartLogService(); } public void Stop() { if (!IsWriting) return; IsWriting = false; logQueue.CompleteAdding(); task?.Wait(); } public void StartLogService() { // 启动日志线程 this.task = Task.Run(() => { while(IsWriting) { // 写入文件 WriteLogByThreadV9(this.logQueue.Take()); } IsWriting=false; }); } public void Enqueue(LogContentV4 log) { logQueue.Add(log); } public void Enqueue(ref List<LogContentV4> logs) { foreach (LogContentV4 log in logs) { this.logQueue.Add(log); } } public void Enqueue(Queue<LogContentV4> rlogs) { foreach (LogContentV4 log in rlogs) { this.logQueue.Add(log); } } public void Enqueue(ref Queue<LogContentV4> rlogs) { while (rlogs.Count > 0) { this.logQueue.Add(rlogs.Dequeue()); } } public void OnLogging() { this.task = Task.Run(() => { IsWriting = true; while (this.logQueue.Count>0) { // 写入文件 WriteLogByThreadV10(); } IsWriting = false; }); } public void WriteLogFile() { using (StreamWriter writer = new StreamWriter(this.logPath.curfilepath, true)) // true表示追加模式 { foreach (var cnt in this.logQueue) { writer.WriteLine(cnt.ToString()); } } } public void WriteErrorLog() { using (StreamWriter writer = new StreamWriter(this.logPath.errorLgFile, true)) // true表示追加模式 { foreach (LogContentV4 cnt in this.logQueue) { if (cnt._Level >= LogLevel.Warning) writer.WriteLine(LogContentV4X.TraceDetail(cnt)); } } } public void WriteLogByThreadV9(LogContentV4 tmp) { this.logStatistic.StartWatch(); //刷新文件路径 this.logPath.CheckAndRotateLogFile(); //FileStream可以设置成独享锁定模式,防止 线程互斥 using (FileStream fs1 = new FileStream(this.logPath.curfilepath, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter writer = new StreamWriter(fs1)) { writer.WriteLine(tmp.ToString()); if (tmp._Level >= LogLevel.Warning) writer.WriteLine(LogContentV4X.TraceDetailV2(tmp)); } } this.logStatistic.StopWatch(); } public void WriteLogByThreadV10() { this.logStatistic.StartWatch(); //刷新文件路径 this.logPath.CheckAndRotateLogFile(); //FileStream可以设置成独享锁定模式,防止 线程互斥 using (FileStream fs1 = new FileStream(this.logPath.curfilepath, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter writer = new StreamWriter(fs1)) { while (this.logQueue.Count > 0) { var tmp = this.logQueue.Take(); writer.WriteLine(tmp.ToString()); if (tmp._Level >= LogLevel.Warning) writer.WriteLine(LogContentV4X.TraceDetailV2(tmp)); } } } this.logStatistic.StopWatch(); } }
http://www.cnnetsun.cn/news/36936.html

相关文章:

  • 版本控制工具介绍及常用命令:Git 与 SVN 入门指南
  • 【多级下拉菜单制作】
  • 13、网络通信控制:SELinux 下的网络安全管理
  • MiniCPM-V 4.5终极部署指南:从环境配置到实战应用全解析
  • 阿里Wan2.1:消费级GPU也能玩转电影级AI视频生成
  • 批量图片压缩神器:支持2GB大文件与万张并发处理
  • px 与 em(简明指南)
  • 如何快速完成ONVIF设备兼容性测试:终极工具使用指南
  • 如何快速获取IEC 61400-1-2019风电设计标准:权威资源下载指南
  • 什么是spring boot
  • 静态文件处理与模板渲染深度指南
  • 从功能测试到自动化架构师:破茧成蝶的技术蜕变之旅
  • 短视频程序源码,实现一个简单的websocket
  • 计算机视觉新突破:OpenCV 5.0新特性解析与软件测试实战
  • 基于SpringBoot + Vue的新能源汽车销售数据分析系统
  • Pock:MacBook Touch Bar Widget管理器的终极解决方案
  • 为什么Hubot Sans成为数字产品设计的颠覆性选择?
  • 基于SpringBoot + Vue的重型机械管理平台
  • Moveable DOM操作库:5个核心场景解决前端交互难题
  • ComfyUI与Zapier集成:触发式自动生产业务流
  • 10、Linux 高级访问控制:ACL 深度解析
  • 12、手动配置 Linux 网络:从路由设置到工具使用的全面指南
  • Walrus去中心化存储系统:新手快速上手指南
  • 30、Python并发编程:线程、进程与调度全解析
  • 34、Python数据持久化:简单与关系序列化的全面解析
  • GPS测量工具终极指南:从零开始掌握精准定位分析
  • 3 SQL注入|数据类型与提交方式|笔记
  • 3D架构设计新体验:iCraft Editor快速上手攻略
  • 25、树莓派多媒体应用全攻略
  • 敏捷开发实战指南:从思维到落地的渐进式掌握