/// <summary>
        /// When overridden in a derived class, participates in rendering operations that are directed by the layout system.
        /// The rendering instructions for this element are not used directly when this method is invoked,
        /// and are instead preserved for later asynchronous use by layout and drawing.
        /// </summary>
        /// <param name="drawingContext">The drawing instructions for a specific element. This context is provided to the layout system.</param>
        protected override void OnRender([NotNull] DrawingContext drawingContext)
        {
            Assert.ArgumentNotNull(drawingContext, "drawingContext");

            try
            {
                if (Visibility != Visibility.Visible)
                {
                    return;
                }

                drawingContext.DrawRectangle(Themes.ThemeManager.Instance.ActiveTheme.GetBrushByTextColor(Themes.TextColor.None, true), new Pen(Themes.ThemeManager.Instance.ActiveTheme.GetBrushByTextColor(Themes.TextColor.None, true), 0), new Rect(0, 0, ActualWidth, ActualHeight));
                var renderedLines = 0;
                if (_selectionSettings.NeedUpdate)
                {
                    _tempSelectionSettings.SelectedMessages.Clear();
                }

                if (_messages.Count > 0)
                {
                    var currentHeight = ActualHeight;
                    var lineNumber    = _currentLineNumber;
                    while (currentHeight > 0 && lineNumber > 0)
                    {
                        _textSource.Message = _messages[lineNumber - 1];
                        var textStorePosition = 0;

                        _linesToRenderStack.Clear();

                        do
                        {
                            var line = _formatter.FormatLine(_textSource, textStorePosition, ActualWidth, _customTextParagraphProperties, null, _textRunCache);
                            _linesToRenderStack.Push(line);
                            textStorePosition += line.Length;
                        }while (textStorePosition < _messages[lineNumber - 1].InnerText.Length);

                        var  drawnChars      = 0;
                        bool messageSelected = false;
                        while (_linesToRenderStack.Count > 0)
                        {
                            var line = _linesToRenderStack.Pop();
                            line.Draw(drawingContext, new Point(0, currentHeight - line.Height), InvertAxes.None);

                            _lineHeight = line.Height;
                            drawnChars += line.Length;
                            if (_selectionSettings.NeedUpdate)
                            {
                                messageSelected = ProcessLineForSelection(line, _messages[lineNumber - 1], currentHeight, drawnChars, messageSelected);
                            }

                            currentHeight -= line.Height;
                            line.Dispose();
                        }

                        renderedLines++;
                        lineNumber--;
                        _textRunCache.Invalidate();
                    }
                }

                _currentNumberOfLinesInView = renderedLines;
                if (_selectionSettings.NeedUpdate)
                {
                    _selectionSettings.SelectionEndCharacterNumber   = _tempSelectionSettings.SelectionEndCharacterNumber;
                    _selectionSettings.SelectionStartCharacterNumber = _tempSelectionSettings.SelectionStartCharacterNumber;
                    _selectionSettings.SelectedMessages.Clear();
                    _selectionSettings.SelectedMessages.AddRange(_tempSelectionSettings.SelectedMessages);
                    _selectionSettings.NeedUpdate = false;
                    InvalidateVisual();
                }
            }
            catch (Exception ex)
            {
                _selectionSettings.NeedUpdate = false;
                _textRunCache.Invalidate();
                ClearTextSelection();
                ErrorLogger.Instance.Write(string.Format("Error rendering text {0}", ex));
            }
        }
 public void Clear()
 {
     textBuilder.Clear();
     spans.Clear();
     cache.Invalidate();
 }