Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added thread for message processing #14

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@
#endregion

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.RichTextBox.Abstraction;



namespace Serilog.Sinks.RichTextBox
{
internal sealed class RichTextBoxSink : ILogEventSink, IDisposable
Expand All @@ -36,42 +42,103 @@ internal sealed class RichTextBoxSink : ILogEventSink, IDisposable
private readonly RenderAction _renderAction;
private const int _defaultWriteBufferCapacity = 256;

private const int _batchSize = 200;
private Thread _consumerThread;
private ConcurrentQueue<LogEvent> _messageQueue;

public RichTextBoxSink(IRichTextBox richTextBox, ITextFormatter formatter, DispatcherPriority dispatcherPriority, object syncRoot)
{
_richTextBox = richTextBox ?? throw new ArgumentNullException(nameof(richTextBox));
_formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));

if (!Enum.IsDefined(typeof(DispatcherPriority), dispatcherPriority))
{
throw new InvalidEnumArgumentException(nameof(dispatcherPriority), (int) dispatcherPriority,
throw new InvalidEnumArgumentException(nameof(dispatcherPriority), (int)dispatcherPriority,
typeof(DispatcherPriority));
}

_dispatcherPriority = dispatcherPriority;
_syncRoot = syncRoot ?? throw new ArgumentNullException(nameof(syncRoot));

_renderAction = Render;
}

public void Emit(LogEvent logEvent)
{
var buffer = new StringWriter(new StringBuilder(_defaultWriteBufferCapacity));
_formatter.Format(logEvent, buffer);
_messageQueue = new ConcurrentQueue<LogEvent>();

var formattedLogEventText = buffer.ToString();
_consumerThread = new Thread(new ThreadStart(ProcessMessages)) { IsBackground = true };
_consumerThread.Start();
}

var xamlParagraphText =
$"<Paragraph xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xml:space=\"preserve\">{formattedLogEventText}</Paragraph>";
private enum States
{
Init,
Dequeue,
Log,
}

var richTextBox = _richTextBox;
private void ProcessMessages()
{
StringBuilder sb = new();
Stopwatch sw = Stopwatch.StartNew();
States state = States.Init;
int msgCounter = 0;

if (!richTextBox.CheckAccess())
while (true)
{
richTextBox.BeginInvoke(_dispatcherPriority, _renderAction, xamlParagraphText);
return;
switch (state)
{
//prepare the string builder and data
case States.Init:
sb.Clear();
sb.Append($"<Paragraph xmlns =\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xml:space=\"preserve\">");
msgCounter = 0;
state = States.Dequeue;
break;

case States.Dequeue:
if (sw.Elapsed.TotalMilliseconds >= 25 || msgCounter >= _batchSize)
{
if (msgCounter == 0)
{
//no messages, retick
sw.Restart();
}
else
{
//valid log condition
state = States.Log;
break;
}
}

if (_messageQueue.TryDequeue(out LogEvent logEvent) == false)
{
Thread.Sleep(1);
continue;
}

StringWriter writer = new();
_formatter.Format(logEvent, writer);

//got a message from the queue, retick
sw.Restart();

msgCounter++;
sb.Append(writer.ToString());
break;

case States.Log:
sb.Append("</Paragraph>");
string xamlParagraphText = sb.ToString();
_richTextBox.BeginInvoke(_dispatcherPriority, _renderAction, xamlParagraphText);
state = States.Init;
break;
}
}
}

Render(xamlParagraphText);
public void Emit(LogEvent logEvent)
{
_messageQueue.Enqueue(logEvent);
}

private void Render(string xamlParagraphText)
Expand Down