news 2026/6/12 5:21:27

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/6/10 18:01:46

验证码暴力破解

这里介绍两中方法第一种&#xff1a;1.随便输入验证码进行抓包2.发送到intruder模块4.设置payload5.分析&#xff08;这里一共1000000条&#xff0c;内容很多&#xff0c;速度慢&#xff09;使用第二种方法绕过直接进入第二种方法&#xff1a;&#xff08;不一定成功&#xff0…

作者头像 李华
网站建设 2026/6/11 6:23:34

性价比高安全体验馆哪家靠谱

探寻性价比高且靠谱的安全体验馆引言在当今注重安全意识培养的时代&#xff0c;安全体验馆成为众多企业和机构提升人员安全素养的重要场所。然而&#xff0c;面对市场上琳琅满目的选择&#xff0c;如何挑选一家性价比高且靠谱的安全体验馆成为关键问题。黑云智能科技&#xff1…

作者头像 李华
网站建设 2026/6/10 0:46:28

8、SELinux 用户登录管理全解析

SELinux 用户登录管理全解析 1. 角色与可访问域 在 SELinux 中,角色定义了与之关联的用户可以访问的域。 seinfo 工具不仅能显示可用角色,还能借助 -x 选项列出某个角色可访问的域。示例如下: # seinfo -rdbadm_r -x dbadm_rDominated Roles:dbadm_rTypes:qmail_inj…

作者头像 李华
网站建设 2026/6/11 11:00:48

如何在5分钟内搭建ZeroTier游戏加速网络?

如何在5分钟内搭建ZeroTier游戏加速网络&#xff1f; 【免费下载链接】ZeroTierOne A Smart Ethernet Switch for Earth 项目地址: https://gitcode.com/GitHub_Trending/ze/ZeroTierOne 还在为游戏联机延迟高、卡顿频繁而烦恼&#xff1f;NAT穿透失败让玩家间的直接连接…

作者头像 李华
网站建设 2026/6/12 3:48:27

Hover Zoom+终极图片预览神器:告别繁琐点击,悬停即放大

Hover Zoom终极图片预览神器&#xff1a;告别繁琐点击&#xff0c;悬停即放大 【免费下载链接】hoverzoom Google Chrome extension for zooming images on mouse hover 项目地址: https://gitcode.com/gh_mirrors/ho/hoverzoom 在当今信息爆炸的时代&#xff0c;网页浏…

作者头像 李华
网站建设 2026/6/10 13:02:17

Open Interface:开启电脑全自动驾驶新时代

Open Interface&#xff1a;开启电脑全自动驾驶新时代 【免费下载链接】Open-Interface Control Any Computer Using LLMs 项目地址: https://gitcode.com/gh_mirrors/op/Open-Interface 在人工智能技术飞速发展的今天&#xff0c;你是否曾想象过你的电脑能够像自动驾驶…

作者头像 李华