Don't thank me, thank Nicko for providing the bulk to the implementation
(GetAppenders and SetTextBox methods).

I wasn't aware of the threading issues involved, so thank you for the
updated code.

-----Original Message-----
From: Paul Welter [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 22, 2004 11:25 PM
To: [email protected]
Subject: TextBoxAppender


John and Neils,
 
Thanks for the TextBoxAppender and RichTextBoxAppender.  They are exactly
what I was looking for.  I have one suggestion though and that is to make
the appenders thread safe.  When using controls, you need to use invoke to
make sure to update the control on the same thread the control was created
on.  See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/htm
l/winforms06112002.asp.  Here is the updated code.
 
------------------------------ BEGIN CODE -----------------------------
private delegate void UpdateControlDelegate(LoggingEvent loggingEvent);
 
private void UpdateControl(LoggingEvent loggingEvent)
{
            // do control updating here
            _textBox.AppendText(Layout.Format(loggingEvent));
}
 
protected override void Append (LoggingEvent LoggingEvent)
{
            // prevent exceptions
            if (_textBox != null && _textBox.Created)
            {
                        // make thread safe
                        if (_textBox.InvokeRequired)
                        {
                                    _textBox.Invoke(
                                                new
UpdateControlDelegate(UpdateControl),
                                                new object[]
{LoggingEvent});
                        }
                        else
                        {
                                    UpdateControl(LoggingEvent);
                        }
            }
}
------------------------------ END CODE -----------------------------
 
thanks again,
Paul

Reply via email to