news 2026/3/1 20:09:11

NtLogPath

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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(); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/1 11:07:08

版本控制工具介绍及常用命令:Git 与 SVN 入门指南

在软件开发过程中&#xff0c;版本控制是不可或缺的一环。它帮助团队管理代码变更、协同开发、回溯历史以及维护项目稳定性。目前最主流的版本控制系统有两个&#xff1a;Git 和 SVN&#xff08;Subversion&#xff09;。本文将带你了解它们的基本概念、核心区别&#xff0c;并…

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

【多级下拉菜单制作】

.men文件 VERSION 120 EDIT UG_GATEWAY_MAIN_MENUBARAFTER UG_FILECASCADE_BUTTON zhuLABEL Ato辅助1.10 END_OF_AFTER MENU zhuCASCADE_BUTTON GEAR_MODELING_BTNLABEL aaaCASCADE_BUTTON SPRING_DESIGN_BTNLABEL bbbCASCADE_BUTTON CN_MANUFACTURE_PREPARE_BTNLABEL cccEND…

作者头像 李华
网站建设 2026/2/28 14:07:55

13、网络通信控制:SELinux 下的网络安全管理

网络通信控制:SELinux 下的网络安全管理 1. 列出连接上下文 管理员可使用多种工具显示安全上下文信息,多数工具借助 -Z 选项实现。以下是一些示例: - netstat :用于列出正在运行的网络绑定服务。 # netstat -naptZ | grep :80 tcp 0 0 0.0.0.0:80 0.0…

作者头像 李华
网站建设 2026/3/1 15:34:39

MiniCPM-V 4.5终极部署指南:从环境配置到实战应用全解析

MiniCPM-V 4.5终极部署指南&#xff1a;从环境配置到实战应用全解析 【免费下载链接】OmniLMM 项目地址: https://gitcode.com/gh_mirrors/om/OmniLMM 在当今多模态AI快速发展的时代&#xff0c;如何在有限硬件资源下部署性能强大的视觉语言模型成为众多开发者的核心痛…

作者头像 李华
网站建设 2026/2/28 9:21:14

阿里Wan2.1:消费级GPU也能玩转电影级AI视频生成

还在为AI视频生成的高门槛发愁吗&#xff1f; 现在&#xff0c;阿里开源的Wan2.1模型彻底改变了游戏规则&#xff0c;让普通用户也能用消费级硬件创作专业级视频内容&#xff01;作为全球首个在消费级GPU上实现720P视频生成的开源模型&#xff0c;它到底有多厉害&#xff1f;&a…

作者头像 李华
网站建设 2026/3/1 10:02:18

批量图片压缩神器:支持2GB大文件与万张并发处理

发现一个处理大量图片的利器。平时整理博客图片或项目截图时&#xff0c;体积太大是个麻烦事&#xff0c;直到用了这款图片压缩器&#xff0c;它处理起上万张图片或单个2GB的大图都很快&#xff0c;效果也很明显。 它最吸引我的地方是性能强悍且操作无脑。直接把一堆图片拖进窗…

作者头像 李华