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(); } }NtLogPath
张小明
前端开发工程师
构建高效可控的测试环境:Docker容器化实践全景解析
一、测试环境演进与容器化价值 随着敏捷开发与持续交付模式的普及,传统测试环境面临资源配置复杂、环境不一致、扩展性受限等痛点。Docker容器化技术通过标准化应用打包与运行环境,为测试团队提供了隔离性、可移植性和弹性伸缩的核心能力。据2025年行业…
SiYuan笔记终极指南:解锁完全开源的知识管理新体验 [特殊字符]
SiYuan笔记终极指南:解锁完全开源的知识管理新体验 🚀 【免费下载链接】siyuan A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. 项目地址: https://gitcode.com/GitHub…
揭秘ShortGPT:AI驱动的短视频自动化创作全攻略
揭秘ShortGPT:AI驱动的短视频自动化创作全攻略 【免费下载链接】ShortGPT 🚀🎬 ShortGPT - Experimental AI framework for automated short/video content creation. 项目地址: https://gitcode.com/gh_mirrors/sh/ShortGPT 还在为视…
GJB9001C-2017质量管理体系要求完整下载指南
GJB9001C-2017质量管理体系要求完整下载指南 【免费下载链接】GJB9001C-2017质量管理体系要求下载 GJB9001C-2017质量管理体系要求下载 项目地址: https://gitcode.com/Open-source-documentation-tutorial/520cb 还在为找不到高质量的GJB9001C-2017质量管理体系要求PDF…
5分钟快速上手AI贴纸生成器:零基础打造专属创意贴纸
5分钟快速上手AI贴纸生成器:零基础打造专属创意贴纸 【免费下载链接】stickerbaker 项目地址: https://gitcode.com/GitHub_Trending/st/stickerbaker 还在为找不到合适的贴纸而烦恼吗?现在只需简单描述想法,AI就能为你生成专属创意贴…
华为 IPD 即集成产品开发(Integrated Product Development),是 1997 年华为耗资数十亿从 IBM 引入,后经本土化创新形成的核心研发管理体系
华为 IPD 即集成产品开发(Integrated Product Development),是 1997 年华为耗资数十亿从 IBM 引入,后经本土化创新形成的核心研发管理体系,其核心是通过组织与流程革新将个人能力转化为组织能力,推动产品从…