news 2026/5/6 18:14:32

NtLogV4

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
NtLogV4
public class NtLogV4 //可能无法使用 { private Queue<LogContentV4> buffer = new Queue<LogContentV4>(); public string LogPath { get; } private string curfilepath = string.Empty; private string errorLgFile = string.Empty; //定义从Exception到Fault这5个层级为Error private Task task; //不要删除,多线程写日志时会用到 private Stopwatch watcher = new Stopwatch(); private long maxms, minms, lastms; private volatile bool IsWriting = false; public bool Writing { get { return IsWriting; } } /// <summary> /// 平均耗时 /// </summary> public long AverageConsum { get { return (maxms + minms + lastms) / 3; } } public NtLogV4() { this.LogPath = AppDomain.CurrentDomain.BaseDirectory; ////创建日志文件夹 this.LogPath = CreateLogDirectory(); MakeLogFileName(); maxms = minms = lastms = 0; } public void Enqueue(LogContentV4 log) { this.buffer.Enqueue(log); } public void Enqueue(ref List<LogContentV4> logs) { foreach (LogContentV4 log in logs) { this.buffer.Enqueue(log); } } public void Enqueue(Queue<LogContentV4> rlogs) { foreach (LogContentV4 log in rlogs) { this.buffer.Enqueue(log); } } public void Enqueue(ref Queue<LogContentV4> rlogs) { while (rlogs.Count > 0) { this.buffer.Enqueue(rlogs.Dequeue()); } } public void OnLogging(ref Queue<LogContentV4> logs) { for (int i = 0; i < logs.Count; i++) { this.buffer.Enqueue(logs.Dequeue()); } this.UpdatePathFileName(); if (this.IsWriting == false && this.buffer.Count > 0) { WriteLogByThreadV5(); } } private static string CreateLogDirectory() { string path = AppDomain.CurrentDomain.BaseDirectory; // 获取当前应用程序域的名称(通常是程序集名称) 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; } private void UpdatePathFileName() { string path = AppDomain.CurrentDomain.BaseDirectory; // 获取当前应用程序域的名称(通常是程序集名称) string assemblyName = AppDomain.CurrentDomain.FriendlyName; // 去掉路径和扩展名,只保留文件名 assemblyName = Path.GetFileNameWithoutExtension(assemblyName); // 组合成完整的路径 path = System.IO.Path.Combine(path, assemblyName + "Log"); //TempLog.logging(path); //创建日志文件夹 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"); } private void MakeLogFileName() { // 获取当前应用程序域的名称(通常是程序集名称) string assemblyName = AppDomain.CurrentDomain.FriendlyName; // 去掉路径和扩展名,只保留文件名 assemblyName = Path.GetFileNameWithoutExtension(assemblyName); 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 void WriteLogFile() { using (StreamWriter writer = new StreamWriter(this.curfilepath, true)) // true表示追加模式 { foreach (var cnt in this.buffer) { writer.WriteLine(cnt.ToString()); } } } public void WriteErrorLog() { using (StreamWriter writer = new StreamWriter(this.errorLgFile, true)) // true表示追加模式 { foreach (LogContentV4 cnt in this.buffer) { if (cnt._Level >= LogLevel.Warning) writer.WriteLine(LogContentV4X.TraceDetail(cnt)); } } } //发现它仍然阻塞主线程 //注意只有一个线程写,不能多个线程同时写文件。 //请注意,Buffer.Remove(cnt) 在循环中可能会导致问题,因为从集合中移除元素会改变集合的大小,从而可能导致迭代器失效。为了避免这个问题,可以先收集需要删除的元素,然后在循环结束后统一删除它们。 public void WriteLogByThreadV5() { this.watcher.Start(); this.IsWriting = true; Queue<LogContentV4> errlogs = new Queue<LogContentV4>(); // 使用Task.Run在后台线程中执行文件写入操作 this.task = Task.Run(() => { //FileStream可以设置成独享锁定模式,防止 线程互斥 using (FileStream fs1 = new FileStream(this.curfilepath, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter writer = new StreamWriter(fs1)) { //foreach (var cnt in queue) //{ // writer.WriteLine(cnt.ToString()); // ; // if (cnt.Level >= LogLevel.Warning) // errlogs.Enqueue(cnt); //} while (this.buffer.Count > 0) { var tmp = this.buffer.Dequeue(); writer.WriteLine(tmp.ToString()); if (tmp._Level >= LogLevel.Warning) errlogs.Enqueue(tmp); } } } //如果没有errlog就不写 if (errlogs.Count > 0) { using (FileStream fs2 = new FileStream(this.errorLgFile, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter writer2 = new StreamWriter(fs2)) { for (int i = 0; i < errlogs.Count; i++) { writer2.WriteLine(LogContentV4X.TraceDetail(errlogs.Dequeue())); } } } } //Buffer没有上锁是希望它尽快完成操作,但有风险 }); this.watcher.Stop(); this.IsWriting = false; if (this.lastms > 0 && this.lastms > this.maxms) { this.maxms = this.lastms; } if (this.lastms > 0 && this.minms == 0) this.minms = this.lastms; if (this.lastms > 0 && this.lastms < this.minms) { this.minms = this.lastms; } this.lastms = watcher.ElapsedMilliseconds; } public void WriteLogByThreadV8() { this.watcher.Start(); this.IsWriting = true; Queue<LogContentV4> errlogs = new Queue<LogContentV4>(); // 使用Task.Run在后台线程中执行文件写入操作 this.task = Task.Run(() => { //刷新文件路径 MakeLogFileName(); //FileStream可以设置成独享锁定模式,防止 线程互斥 using (FileStream fs1 = new FileStream(this.curfilepath, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter writer = new StreamWriter(fs1)) { //foreach (var cnt in queue) //{ // writer.WriteLine(cnt.ToString()); // ; // if (cnt.Level >= LogLevel.Warning) // errlogs.Enqueue(cnt); //} while (this.buffer.Count > 0) { var tmp = this.buffer.Dequeue(); writer.WriteLine(tmp.ToString()); if (tmp._Level >= LogLevel.Warning) errlogs.Enqueue(tmp); } } } //如果没有errlog就不写 if (errlogs.Count > 0) { using (FileStream fs2 = new FileStream(this.errorLgFile, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter writer2 = new StreamWriter(fs2)) { for (int i = 0; i < errlogs.Count; i++) { writer2.WriteLine(LogContentV4X.TraceDetailV2(errlogs.Dequeue())); } } } } //Buffer没有上锁是希望它尽快完成操作,但有风险 }); this.watcher.Stop(); this.IsWriting = false; if (this.lastms > 0 && this.lastms > this.maxms) { this.maxms = this.lastms; } if (this.lastms > 0 && this.minms == 0) this.minms = this.lastms; if (this.lastms > 0 && this.lastms < this.minms) { this.minms = this.lastms; } this.lastms = watcher.ElapsedMilliseconds; } public void WriteLogByThreadV8(Queue<LogContentV4> logqueue) { Enqueue(logqueue); this.watcher.Start(); this.IsWriting = true; Queue<LogContentV4> errlogs = new Queue<LogContentV4>(); // 使用Task.Run在后台线程中执行文件写入操作 this.task = Task.Run(() => { //刷新文件路径 MakeLogFileName(); //FileStream可以设置成独享锁定模式,防止 线程互斥 using (FileStream fs1 = new FileStream(this.curfilepath, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter writer = new StreamWriter(fs1)) { //foreach (var cnt in queue) //{ // writer.WriteLine(cnt.ToString()); // ; // if (cnt.Level >= LogLevel.Warning) // errlogs.Enqueue(cnt); //} while (this.buffer.Count > 0) { var tmp = this.buffer.Dequeue(); writer.WriteLine(tmp.ToString()); if (tmp._Level >= LogLevel.Warning) errlogs.Enqueue(tmp); } } } //如果没有errlog就不写 if (errlogs.Count > 0) { using (FileStream fs2 = new FileStream(this.errorLgFile, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter writer2 = new StreamWriter(fs2)) { for (int i = 0; i < errlogs.Count; i++) { writer2.WriteLine(LogContentV4X.TraceDetailV2(errlogs.Dequeue())); } } } } //Buffer没有上锁是希望它尽快完成操作,但有风险 }); this.watcher.Stop(); this.IsWriting = false; if (this.lastms > 0 && this.lastms > this.maxms) { this.maxms = this.lastms; } if (this.lastms > 0 && this.minms == 0) this.minms = this.lastms; if (this.lastms > 0 && this.lastms < this.minms) { this.minms = this.lastms; } this.lastms = watcher.ElapsedMilliseconds; } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 6:41:38

企业级应用中的JRE部署最佳实践

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个企业JRE管理系统的原型&#xff0c;功能包括&#xff1a;1.多版本JRE集中管理 2.自动更新检测 3.安全漏洞扫描 4.使用情况统计 5.远程部署功能。系统需要支持LDAP集成&…

作者头像 李华
网站建设 2026/5/3 9:56:20

无需安装!在线体验n8n的5种创新方法

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个n8n快速体验平台&#xff0c;集成以下功能&#xff1a;1) 基于Web的n8n实例&#xff08;预配置常用连接器&#xff09;&#xff1b;2) 示例工作流库&#xff08;分类展示&a…

作者头像 李华
网站建设 2026/5/2 18:00:55

AI如何革新Keil开发?智能代码补全与调试实战

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个基于Keil MDK的智能辅助开发工具&#xff0c;集成AI代码补全、语法检查、错误预测功能。要求&#xff1a;1.支持STM32系列芯片的C语言自动补全 2.实时检测常见嵌入式编程错…

作者头像 李华
网站建设 2026/5/3 9:32:40

前11个月我国货物贸易进出口增长3.6%

据海关统计&#xff0c;2025年前11个月&#xff0c;我国货物贸易进出口总值41.21万亿元人民币&#xff0c;同比&#xff08;下同&#xff09;增长3.6%。其中&#xff0c;出口24.46万亿元&#xff0c;增长6.2%&#xff1b;进口16.75万亿元&#xff0c;增长0.2%。11月份&#xff…

作者头像 李华
网站建设 2026/5/6 3:14:36

提升资源管理效率必备工具推荐

一、数据传输1、Textify&#xff08;PC&#xff09;作为一名技术人员&#xff0c;需要经常研究各种技术&#xff0c;因此&#xff0c;复制粘贴的操作十分常见。Textify可以快速复制各种控件中的文本&#xff0c;包括浏览器中当前页面的标题、聊天窗口中各个部分的文字、桌面应用…

作者头像 李华
网站建设 2026/5/4 22:55:00

基于Springboot监狱罪犯危险性评估系统【附源码+文档】

&#x1f495;&#x1f495;作者&#xff1a; 米罗学长 &#x1f495;&#x1f495;个人简介&#xff1a;混迹java圈十余年&#xff0c;精通Java、小程序、数据库等。 &#x1f495;&#x1f495;各类成品Java毕设 。javaweb&#xff0c;ssm&#xff0c;springboot等项目&#…

作者头像 李华