Skip to content

Commit ab2081e

Browse files
committed
Fixed bugs in the message consumer
1 parent 6195c6f commit ab2081e

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
$coverage = Select-Xml -Path "./coverage/**/coverage.cobertura.xml" -XPath "//coverage/@line-rate" | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty Value
5151
$coveragePercent = [math]::Round([double]$coverage * 100, 2)
5252
Write-Host "Current line coverage: $coveragePercent%"
53-
if ($coveragePercent -lt 70) {
53+
if ($coveragePercent -lt 75) {
5454
Write-Error "Code coverage ($coveragePercent%) is below the required threshold of 70%"
5555
exit 1
5656
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Extensions/RichTextBoxExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ private struct Point
4040
#if NET7_0_OR_GREATER
4141
// Use source-generated P/Invoke for newer target frameworks to avoid the startup
4242
// penalty of runtime marshalling stub generation.
43-
[LibraryImport("user32.dll")]
43+
[LibraryImport("user32.dll", EntryPoint = "SendMessageW")]
4444
private static partial IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
4545

46-
[LibraryImport("user32.dll")]
46+
[LibraryImport("user32.dll", EntryPoint = "SendMessageW")]
4747
private static partial IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, ref Point lParam);
4848
#else
4949
// Fallback for older target frameworks that do not support source-generated P/Invokes.

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,34 @@ public void Dispose()
7777
public void Emit(LogEvent logEvent)
7878
{
7979
_buffer.Add(logEvent);
80-
Interlocked.Exchange(ref _hasNewMessages, 1);
81-
_signal.Set();
80+
if (Interlocked.Exchange(ref _hasNewMessages, 1) == 0)
81+
{
82+
_signal.Set();
83+
}
8284
}
8385

8486
private void ProcessMessages(CancellationToken token)
8587
{
8688
var builder = new RtfBuilder(_options.Theme);
8789
var snapshot = new System.Collections.Generic.List<LogEvent>(_options.MaxLogLines);
90+
var lastFlush = DateTime.UtcNow;
8891

8992
while (!token.IsCancellationRequested)
9093
{
9194
_signal.WaitOne(_options.FlushInterval);
95+
if (Interlocked.CompareExchange(ref _hasNewMessages, 1, 1) == 0)
96+
{
97+
continue;
98+
}
9299

93-
if (Interlocked.Exchange(ref _hasNewMessages, 0) == 0)
100+
var now = DateTime.UtcNow;
101+
var elapsed = now - lastFlush;
102+
if (elapsed < _options.FlushInterval)
94103
{
95104
continue;
96105
}
97106

107+
Interlocked.Exchange(ref _hasNewMessages, 0);
98108
_buffer.TakeSnapshot(snapshot);
99109
builder.Clear();
100110
foreach (var evt in snapshot)
@@ -103,6 +113,7 @@ private void ProcessMessages(CancellationToken token)
103113
}
104114

105115
_richTextBox.SetRtf(builder.Rtf, _options.AutoScroll);
116+
lastFlush = now;
106117
}
107118
}
108119
}

0 commit comments

Comments
 (0)