首頁  >  文章  >  後端開發  >  WPF MaterialDesign 範例開源專案介紹

WPF MaterialDesign 範例開源專案介紹

零下一度
零下一度原創
2017-06-23 15:01:434262瀏覽

 

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;// 10sstatic 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 。

 

當然,期待你們更好的建議,大家一起學習,你有好的想法,自己又不想寫,我來幫你實現!


以上是WPF MaterialDesign 範例開源專案介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn