news 2026/1/9 8:49:05

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/1/5 0:25:24

React小白也能懂:useEffect入门图解指南

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 生成一个面向初学者的useEffect教学示例&#xff0c;要求&#xff1a;1) 使用最简单的计数器demo展示基本用法&#xff1b;2) 添加可视化流程图解释执行时机&#xff1b;3) 包含常见…

作者头像 李华
网站建设 2026/1/5 13:24:12

电商网站遇到Internal Server Error的应急处理方案

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个模拟电商网站的高并发测试工具&#xff0c;能够模拟用户请求并记录服务器响应。当检测到Internal Server Error时&#xff0c;自动生成报告&#xff0c;包括错误发生的时间…

作者头像 李华
网站建设 2026/1/4 21:54:39

基于微信小程序+node.js的校园餐饮系统设计与实现

前言 &#x1f31e;博主介绍&#xff1a;✌CSDN特邀作者、全栈领域优质创作者、10年IT从业经验、码云/掘金/知乎/B站/华为云/阿里云等平台优质作者、专注于Java、小程序/APP、python、大数据等技术领域和毕业项目实战&#xff0c;以及程序定制化开发、文档编写、答疑辅导等。✌…

作者头像 李华
网站建设 2026/1/5 6:20:37

springboot基于vue的大学生公益活动志愿服务系统的设计与实现_nahamqu8

目录已开发项目效果实现截图开发技术系统开发工具&#xff1a;核心代码参考示例1.建立用户稀疏矩阵&#xff0c;用于用户相似度计算【相似度矩阵】2.计算目标用户与其他用户的相似度系统测试总结源码文档获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&…

作者头像 李华
网站建设 2026/1/3 14:31:30

深度学习入门:图像分类的实战应用

个人首页&#xff1a; 永远都不秃头的程序员(互关) C语言专栏:从零开始学习C语言 C专栏:C的学习之路 本文章所属专栏&#xff1a;人工智能从 0 到 1&#xff1a;普通人也能上手的实战指南 目录 引言 一、图像分类的基本概念 1. 什么是图像分类&#xff1f; 2. 深度学习…

作者头像 李华