This article is a mirror article of machine translation, please click here to jump to the original article.

View: 41603|Reply: 2

[Source] .NET/C# implements simple logging based on BlockingCollection

[Copy link]
Posted on 2021-1-5 14:58:50 | | | |
I wrote a simple logging help class before, copied from another framework, and the test is not easy to use, as follows:

C# is the easiest way to write logs
https://www.itsvse.com/thread-5111-1-1.html
This article reimplements a simple logging class based on BlockingCollection.

.NET/C# Thread Safety Collection (collation)
https://www.itsvse.com/thread-3769-1-1.html
BlockingCollection<T> is a thread-safe collection class that provides the following functionality:

Implement the maker-user model.
Add and fetch items concurrently through multithreading.
Optional maximum capacity.
When the collection is empty or full, it is blocked by insert and remove operations.
Insert and remove attempts do not block or occur for a specified period of time.
Encapsulation implements any collection type of IProducerConsumerCollection<T>
Use the cancel tag to perform the cancellation action.
Two types of enums that use foreach (in Visual Basic, For Each) are supported: read-only enums. An enumeration that removes an item when an item is enumerated.


Microsoft introduces:The hyperlink login is visible.

BlockingCollection is a thread-safe blocking queue, using this feature to implement the producer-consumer mode within the process, such as message forwarding, logging, etc.

Call its Add method to add information to the queue, and when the queue has no messages, IsCompleted will always block the while loop, so it will not cause a dead loop. When a new message comes out, the IsCompleted in while will be triggered and return the bool type, causing the execution to continue, that is, fetching data from the thread.


Previously, we blocked the thread by calling the WaitOne method of ManualResetEvent, calling the Reset and Set methods to pass the signal, as follows:

c# encapsulates a universal QueueHelper queue
https://www.itsvse.com/thread-5029-1-1.html


The test writes 10,000 logs, which takes less than 10 milliseconds, as shown in the figure below:



The code is as follows:

Source code download:

Tourists, if you want to see the hidden content of this post, pleaseReply





Previous:warning: LF will be replaced by CRLF in xxxx.
Next:Entity Framework Query Optimization WITH(NOLOCK)
 Landlord| Posted on 2021-1-23 11:53:55 |
.NET/C# is based on the BlockingCollection background task queue
https://www.itsvse.com/thread-9528-1-1.html

Posted on 2023-4-24 22:36:31 |
Excellent, very good study material
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com