I would like to use a RichTextBox WPF control to print log outputs, but I am wondering what is the most efficient way to remove the "oldest" lines after there are for example more than 10,000 lines displayed, if it is possible to actually implement a queue开发者_如何学Go behaviour, especially as there is no easy "Text" property to play with.
Unfortunately, I am not able to achieve this result with nlog either probably due to a bug or a limitation.
You can use a Queue<string>
to store your log messages, and display them using the RichTextBox.Lines
property.
For example:
private const int _maxCapacity = 10000;
private Queue<string> _messageQueue = new Queue<string>(_maxCapacity);
private void button1_Click(object sender, EventArgs e)
{
if (_messageQueue.Count >= _maxCapacity)
{
_messageQueue.Dequeue();
}
_messageQueue.Enqueue("message " + _count++.ToString());
richTextBox1.Lines = _messageQueue.ToArray();
}
If you want the most recent messages to appear on top, reverse the queue:
richTextBox1.Lines = _messageQueue.Reverse().ToArray();
If you extend the original control with a LineCount int property, it is possible to use this (inspired by some code given here):
if (this.MaxLines > 0)
{
this.lineCount++;
if (this.lineCount > this.MaxLines)
{
tr = new TextRange(rtbx.Document.ContentStart, rtbx.Document.ContentEnd);
tr.Text = tr.Text.Remove(0, tr.Text.IndexOf('\n'));
this.lineCount--;
}
}
//And for auto scrolling
if (this.AutoScroll)
{
rtbx.ScrollToEnd();
}
精彩评论