WPF materialdesign Sample Open Source project Introduction

Source: Internet
Author: User
Hello all, I'm back.

This time we are really starting to talk about the development of small and useful modules or components in open source projects.

At the same time, the software has been updated to 1.60 version, support the new user registration, you can no longer use the unified test account.

You can download it from the following path:

1, fellow on GitHub on the project, download to the local, generate, you can get the latest version of the program.

2, the local beta v0.5 version of the user can directly in the upper right corner of the program click Update

3, finally to the non-Microsoft Department developer is the following compression package, directly decompression can be used.


If you don't know what the software is, you can check out this blog post:

WPF materialdesign Sample Open Source project work time Manager

Today, we talk about the client's log component.

I don't know if it's a fit, but it's part of the software that can be reused, and I call it a component.

This time the log class is a very typical component, the log has many characteristics;

1, will be used in any part of the software

2, will be called at any time

3. Very high usage rate

4. Frequent IO operations

When I was just in touch with C #, I used log4net, but the idea was that I could have a program that was just a few m in size, and a log component would be more than a master program, which is obviously inappropriate.

So I didn't graduate two years ago. I started to do my first log component.

". NET" creates its own log component--improved version

The basic idea is still good, including: threading, blocking, resource competition and so on have done some consideration.

As the saying goes, dictionary, what do not know very much of their own so open dry.

Wrote the first version of the log component that was made by the thread.

But, after all, young, the problem is obvious. 100 a second is not going to make it.

So when I started the development of C #, I took some time to reconstruct it.

First, start with the overall architecture: -Legacy Component features:* Use a multi-threaded queue to control the writing of the thread to the text with the mutex variable.
* Control the contention of resources by single case lock thread is randomly selected into the lock, write the log time sequence may not be * a thread once a text operation, the switch is in one thread operation, write only one variable at a time-advantages: * Multi-threaded operation, surface improve operational efficiency * Single case lock, ensure the only-missing Point: * Performance under, over-operation IO results in severe performance redundancy * Write one at a time-improved * Use producer consumer mode, separate operation, limit number of threads * Use stack queue, FIFO, guaranteed log order * Single IO variable, batch write operation transformation results:
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 Q Ueue ();p rivate static bool _isstreamclose = True;private static bool _isthreadbegin = false;private static StreamWriter _f Ilestreamwriter;private static readonly string fileName =@ "BugLog.txt"; static int _intervaltime = 10000;//10sstatic Syst Em. Timers.timer _timer = new System.Timers.Timer (_intervaltime);///<summary>///Add journal queue//</summary>///< param name= "message" ></param>public static void Addlog (String message) {string logcontent = $ "[{DATETIME.N            Ow: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}] Error occurred in: {EX . Source},\r\N content: {ex.            Message} "; Logcontent + = $ "\ r \ n tracking: {ex.            StackTrace} ";            Logqueue.enqueue (logcontent); if (!_isthreadbegin) {beginthread (); }///<summary>///reads a data from the log queue///</summary>///<returns></returns>private static Object G        Etlog () {return logqueue.dequeue (); }///<summary>///turn on timed query thread///</summary>public static void Beginthread () {_isthreadbegin = true;//instantiates the Timer class, setting a time interval of 10000 milliseconds; _timer.            Interval = _intervaltime; _timer. Elapsed + = setlog;//Execution event at time of arrival; _timer. AutoReset = true;//Whether the setting is executed once (false) or always (true); _timer.        Enabled = true; }///<summary>///Write Log///</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>///Determine if there is a log file///</summary>private static void Isexist () {String path = Environment.curr Entdirectory +@ "\log\"; if (!            File.exists (Path)) {directory.createdirectory (path); }        }    }}

The code does not have a third-party component application and can be used to copy the file directly.

For now, there are no special cases to handle, such as log file occupancy, software temporary shutdown, and queue trigger time and batch write count, this is just a basic demo,

Of course, if you want to know the following improvements, you can focus on GitHub for the project.

Of course, look forward to your better suggestions, we study together, you have a good idea, you do not want to write, I will help you achieve!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.