Skip to content

Commit 8fa1007

Browse files
committed
Add clear and restore functionality to RichTextBoxSink
1 parent 9b1453c commit 8fa1007

File tree

4 files changed

+104
-15
lines changed

4 files changed

+104
-15
lines changed

Demo/Form1.Designer.cs

Lines changed: 32 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Demo/Form1.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace Demo
3333
public partial class Form1 : Form
3434
{
3535
private RichTextBoxSinkOptions? _options;
36+
private RichTextBoxSink? _sink;
3637
private bool _toolbarsVisible = true;
3738

3839
public Form1()
@@ -47,10 +48,10 @@ private void Initialize()
4748
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:l}{NewLine}{Exception}",
4849
formatProvider: new CultureInfo("en-US"));
4950

50-
var sink = new RichTextBoxSink(richTextBox1, _options);
51+
_sink = new RichTextBoxSink(richTextBox1, _options);
5152
Log.Logger = new LoggerConfiguration()
5253
.MinimumLevel.Verbose()
53-
.WriteTo.Sink(sink, LogEventLevel.Verbose)
54+
.WriteTo.Sink(_sink, LogEventLevel.Verbose)
5455
.CreateLogger();
5556

5657
Log.Debug("Started logger.");
@@ -337,5 +338,25 @@ private void Form1_KeyDown(object sender, KeyEventArgs e)
337338
toolStrip2.Visible = _toolbarsVisible;
338339
}
339340
}
341+
342+
private void btnClear_Click(object sender, EventArgs e)
343+
{
344+
if (_sink == null)
345+
{
346+
return;
347+
}
348+
349+
_sink.Clear();
350+
}
351+
352+
private void btnRestore_Click(object sender, EventArgs e)
353+
{
354+
if (_sink == null)
355+
{
356+
return;
357+
}
358+
359+
_sink.Restore();
360+
}
340361
}
341362
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Collections/ConcurrentCircularBuffer.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
namespace Serilog.Sinks.RichTextBoxForms.Collections
44
{
5-
internal sealed class ConcurrentCircularBuffer<T>
5+
public sealed class ConcurrentCircularBuffer<T>
66
{
77
private readonly object _sync = new();
88
private readonly T[] _buffer;
99
private readonly int _capacity;
1010
private int _head;
1111
private int _count;
12+
private int _itemsToSkip;
1213

1314
public ConcurrentCircularBuffer(int capacity)
1415
{
1516
_capacity = capacity > 0 ? capacity : 1;
1617
_buffer = new T[_capacity];
18+
_itemsToSkip = 0;
1719
}
1820

1921
public void Add(T item)
@@ -33,6 +35,11 @@ public void Add(T item)
3335
{
3436
_head = 0;
3537
}
38+
39+
if (_itemsToSkip > 0)
40+
{
41+
_itemsToSkip--;
42+
}
3643
}
3744
else
3845
{
@@ -47,9 +54,15 @@ public void TakeSnapshot(List<T> target)
4754
{
4855
target.Clear();
4956

50-
for (var i = 0; i < _count; ++i)
57+
var itemsToTake = _count - _itemsToSkip;
58+
if (itemsToTake <= 0)
5159
{
52-
var index = _head + i;
60+
return;
61+
}
62+
63+
for (var i = 0; i < itemsToTake; ++i)
64+
{
65+
var index = _head + _itemsToSkip + i;
5366
if (index >= _capacity)
5467
{
5568
index -= _capacity;
@@ -59,5 +72,21 @@ public void TakeSnapshot(List<T> target)
5972
}
6073
}
6174
}
75+
76+
public void Clear()
77+
{
78+
lock (_sync)
79+
{
80+
_itemsToSkip = _count;
81+
}
82+
}
83+
84+
public void Restore()
85+
{
86+
lock (_sync)
87+
{
88+
_itemsToSkip = 0;
89+
}
90+
}
6291
}
6392
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/RichTextBoxSink.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ public void Dispose()
7878
public void Emit(LogEvent logEvent)
7979
{
8080
_buffer.Add(logEvent);
81+
Update();
82+
}
83+
84+
public void Clear()
85+
{
86+
_buffer.Clear();
87+
Update();
88+
}
89+
90+
public void Restore()
91+
{
92+
_buffer.Restore();
93+
Update();
94+
}
95+
96+
private void Update()
97+
{
8198
Interlocked.Exchange(ref _hasNewMessages, 1);
8299
_signal.Set();
83100
}
@@ -107,8 +124,6 @@ private void ProcessMessages(CancellationToken token)
107124
}
108125

109126
Interlocked.Exchange(ref _hasNewMessages, 0);
110-
111-
// Take a snapshot of the current buffer
112127
_buffer.TakeSnapshot(snapshot);
113128
builder.Clear();
114129
foreach (var evt in snapshot)

0 commit comments

Comments
 (0)