NtLogPath
2026/6/30 0:53:55 网站建设 项目流程
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(); } }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询