Hello all , 我又回來了 這次我們真是開始來聊聊開源項目里,小而有用的模塊或者組件的開發思想。 同時,軟體已經更新到1.60的版本了,支持新用戶註冊,可以不再使用統一的test賬戶了。 您可以通過以下路徑進行下載: 1、在GitHub上fellow一下項目,下載到本地,生成一下,即可獲取最 ...
Hello all , 我又回來了
這次我們真是開始來聊聊開源項目里,小而有用的模塊或者組件的開發思想。
同時,軟體已經更新到1.60的版本了,支持新用戶註冊,可以不再使用統一的test賬戶了。
您可以通過以下路徑進行下載:
1、在GitHub上fellow一下項目,下載到本地,生成一下,即可獲取最新版程式。
2、本地是beta v0.5版本的用戶可以直接在程式右上角點擊更新
3、最後給非微軟系開發者的是以下的壓縮包,直接解壓即可使用。
如果你還不知道是什麼軟體,可以查看這篇博文:
【WPF MaterialDesign 示例開源項目】 Work Time Manager
今天,我們聊聊客戶端的日誌組件。
我也不知道說組件合不合適,反正就是屬於軟體一部分並且可以被重覆利用的小模塊我稱之為組件。
這次的日誌類就是很典型的一個組件,日誌有很多特點;
1、會被使用在軟體的任意一個地方
2、隨時都會被調用
3、使用率極高
4、頻繁的io操作
在我剛剛接觸c#的時候,就使用過了Log4net,但是,那時候就萌生的想法是,我一個程式可能也才幾m大小,一個日誌組件就比上我一個主程式了,這明顯是不合適的。
於是兩年前還沒有畢業的我著手做了自己的第一個日誌組件。
【.net】創建屬於自己的log組件——改進版
基礎的思想還是好的,包括:線程,阻塞,資源競爭等都做了一定的考慮。
俗話說初生牛犢不怕虎,啥也不太知道的自己就這麼開幹了。
寫了第一版通過開線程來做的日誌組件。
可是,畢竟年輕,問題還是顯而易見的。一秒打100條就不行了。
於是在我重新著手c#開發的時候,抽了點時間,來了一次重構。
首先,從整體架構下手: - 舊組件特點: * 使用多線程隊列,用互斥變數控制線程對文本的寫入。
* 通過單例加鎖的方式,控制資源的爭奪 * 線程是隨機被選中入鎖的,寫入的日誌時間順序可能不對 * 一個線程一次文本操作,開關都在一個線程操作,一次只寫入一條變數 - 優點: * 多線程操作,錶面提高操作效率 * 單例加鎖,確保唯一 - 缺點: * 性能底下,過度操作io導致性能嚴重冗餘 * 一次只寫一條 - 改進 * 使用生產者消費者模式,分開操作,限制線程數量 * 使用棧隊列,先進先出,保證日誌順序 * 單例IO變數,批量進行寫入操作 改造成果:
using System; using System.Collections; using System.IO; using System.Text; using System.Threading; using System.Windows.Threading; namespace Helper { public static class LogHelper { private static readonly Queue LogQueue = new Queue(); private static bool _isStreamClose = true; private static bool _isThreadBegin = false; private static StreamWriter _fileStreamWriter; private static readonly string fileName =@"BugLog.txt"; static int _intervalTime = 10000;// 10s static System.Timers.Timer _timer = new System.Timers.Timer(_intervalTime); /// <summary> /// 添加日誌隊列 /// </summary> /// <param name="message"></param> public static void AddLog(string message) { string logContent = $"[{DateTime.Now:yyyy-MM-dd hh:mm:ss}] =>{message}"; LogQueue.Enqueue(logContent); if (!_isThreadBegin) { BeginThread(); } } public static void AddLog(Exception ex) { var logContent = $"[{DateTime.Now:yyyy-MM-dd hh:mm:ss}]錯誤發生在:{ex.Source},\r\n 內容:{ex.Message}"; logContent += $"\r\n 跟蹤:{ex.StackTrace}"; LogQueue.Enqueue(logContent); if (!_isThreadBegin) { BeginThread(); } } /// <summary> /// 讀取日誌隊列的一條數據 /// </summary> /// <returns></returns> private static object GetLog() { return LogQueue.Dequeue(); } /// <summary> /// 開啟定時查詢線程 /// </summary> public static void BeginThread() { _isThreadBegin = true; //實例化Timer類,設置間隔時間為10000毫秒; _timer.Interval = _intervalTime; _timer.Elapsed += SetLog; //到達時間的時候執行事件; _timer.AutoReset = true; //設置是執行一次(false)還是一直執行(true); _timer.Enabled = true; } /// <summary> /// 寫入日誌 /// </summary> private static void SetLog(object source, System.Timers.ElapsedEventArgs e) { if (LogQueue.Count == 0) { if (_isStreamClose) return; _fileStreamWriter.Flush(); _fileStreamWriter.Close(); _isStreamClose = true; return; } if (_isStreamClose) { Isexist(); string errLogFilePath = Environment.CurrentDirectory + @"\Log\" + fileName.Trim(); if (!File.Exists(errLogFilePath)) { FileStream fs1 = new FileStream(errLogFilePath, FileMode.Create, FileAccess.Write); _fileStreamWriter = new StreamWriter(fs1); } else { _fileStreamWriter = new StreamWriter(errLogFilePath, true); } _isStreamClose = false; } var strLog = new StringBuilder(); var onceTime = 50; var lineNum = LogQueue.Count > onceTime ? onceTime : LogQueue.Count; for (var i = 0; i < lineNum; i++) { strLog.AppendLine(GetLog().ToString()); } _fileStreamWriter.WriteLine(strLog.ToString()); } /// <summary> /// 判斷是否存在日誌文件 /// </summary> private static void Isexist() { string path = Environment.CurrentDirectory + @"\Log\"; if (!File.Exists(path)) { Directory.CreateDirectory(path); } } } }
代碼沒有第三方組件的應用,直接把這個文件複製即可使用。
現在暫時沒有對一些特殊情況做處理,例如日誌文件被占用、軟體臨時關閉,以及隊列觸發時間和批量寫入個數等考慮,這隻是一個最基礎的demo,
當然,如果你想知道後續的改進方法,可以關註該項目的GitHub 。
當然,期待你們更好的建議,大家一起學習,你有好的想法,自己又不想寫,我來幫你實現!
歡迎大家狂噴,只有批評才是前進最好的動力!
同時:WorkTimeManager的線上API 即將開放,歡迎各位開發者開發對應的周邊應用哦!
敬請關註:http://api.timemanager.online/