Hello all , 我又回來了html
此次咱們真是開始來聊聊開源項目裏,小而有用的模塊或者組件的開發思想。git
同時,軟件已經更新到1.60的版本了,支持新用戶註冊,能夠再也不使用統一的test帳戶了。github
您能夠經過如下路徑進行下載:c#
一、在GitHub上fellow一下項目,下載到本地,生成一下,便可獲取最新版程序。api
二、本地是beta v0.5版本的用戶能夠直接在程序右上角點擊更新多線程
三、最後給非微軟系開發者的是如下的壓縮包,直接解壓便可使用。架構
【點擊下載】post
若是你還不知道是什麼軟件,能夠查看這篇博文:性能
今天,咱們聊聊客戶端的日誌組件。學習
我也不知道說組件合不合適,反正就是屬於軟件一部分而且能夠被重複利用的小模塊我稱之爲組件。
此次的日誌類就是很典型的一個組件,日誌有不少特色;
一、會被使用在軟件的任意一個地方
二、隨時都會被調用
三、使用率極高
四、頻繁的io操做
在我剛剛接觸c#的時候,就使用過了Log4net,可是,那時候就萌生的想法是,我一個程序可能也才幾m大小,一個日誌組件就比上我一個主程序了,這明顯是不合適的。
因而兩年前尚未畢業的我着手作了本身的第一個日誌組件。
基礎的思想仍是好的,包括:線程,阻塞,資源競爭等都作了必定的考慮。
俗話說初生牛犢不怕虎,啥也不太知道的本身就這麼開幹了。
寫了初版經過開線程來作的日誌組件。
但是,畢竟年輕,問題仍是顯而易見的。一秒打100條就不行了。
因而在我從新着手c#開發的時候,抽了點時間,來了一次重構。
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/