Paul,
  Thanks for the fix,  I've incorporated it into the RichTextBoxAppender.
Hopefully I did it correctly :-)

Thanks,

John Cole

------------------------------ BEGIN CODE -----------------------------
using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;

namespace ReferenceViewer
{
        /// <summary>
        /// Description of RichTextBoxAppender.
        /// </summary>
        public class RichTextBoxAppender : AppenderSkeleton
        {
                private System.Windows.Forms.RichTextBox _richtextBox;

                public RichTextBoxAppender()
                {
                }
                
                private delegate void
UpdateControlDelegate(log4net.spi.LoggingEvent loggingEvent);

                private void UpdateControl(log4net.spi.LoggingEvent
loggingEvent)
                {
                        // I looked at the TortoiseCVS code to figure out
how
                        // to use the RTB as a colored logger.  It noted a
performance
                        // problem when the buffer got long, so it cleared
it every 100K.
                        if(_richtextBox.TextLength > 100000) 
                        {
                                _richtextBox.Clear();
                                _richtextBox.SelectionColor = Color.Gray;
                                _richtextBox.AppendText("(earlier messages
cleared because of log length)\n\n");
                        }
                        
                        switch(loggingEvent.Level.ToString()) {
                                case "INFO":
                                        _richtextBox.SelectionColor =
System.Drawing.Color.Black;
                                        break;
                                case "WARN":
                                        _richtextBox.SelectionColor =
System.Drawing.Color.Blue;
                                        break;
                                case "ERROR":
                                        _richtextBox.SelectionColor =
System.Drawing.Color.Red;
                                        break;
                                case "FATAL":
                                        _richtextBox.SelectionColor =
System.Drawing.Color.DarkOrange;
                                        break;
                                case "DEBUG":
                                        _richtextBox.SelectionColor =
System.Drawing.Color.DarkGreen;
                                        break;
                                default:
                                        _richtextBox.SelectionColor =
System.Drawing.Color.Black;
                                        break;
                        }
        
_richtextBox.AppendText(Layout.Format(loggingEvent));
                }

                protected override void Append (log4net.spi.LoggingEvent
LoggingEvent)
                {
                        // prevent exceptions
                        //if (_richtextBox != null && _richtextBox.Created)
                        if (_richtextBox != null)
                        {
                                // make thread safe
                                if (_richtextBox.InvokeRequired)
                                {
                                        _richtextBox.Invoke(
                                                new
UpdateControlDelegate(UpdateControl),
                                                new object[]
{LoggingEvent});
                                } else {
                                        UpdateControl(LoggingEvent);
                                }
                        }
                }


                public RichTextBox RichTextBox
                {
                        set 
                        {
                                _richtextBox = value;
                        }
                        get
                        {
                                return _richtextBox;
                        }
                }

                public static void SetRichTextBox(RichTextBox rtb)
                {
                        rtb.ReadOnly = true;
                        rtb.HideSelection = false;      // allows rtb to
allways append at the end
                        rtb.Clear();

                        foreach(log4net.Appender.IAppender appender in
GetAppenders())
                        {
                                if (appender is RichTextBoxAppender)
                                {
        
((RichTextBoxAppender)appender).RichTextBox = rtb;
                                }
                        }
                }

                private static IAppender[] GetAppenders()
                {
                        ArrayList appenders = new ArrayList();

        
        
appenders.AddRange(((Hierarchy)LogManager.GetLoggerRepository()).Root.Append
ers);

                        foreach(ILog log in LogManager.GetCurrentLoggers())
                        {
        
appenders.AddRange(((Logger)log.Logger).Appenders);
                        }

                        return
(IAppender[])appenders.ToArray(typeof(IAppender));
                }

        }
}
------------------------------ BEGIN 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

-------------------------------------
This email and any files transmitted with it are confidential and intended 
solely for the use of the individual or entity to whom they are addressed. If 
you have received this email in error please notify the system manager. This 
message contains confidential information and is intended only for the 
individual named. If you are not the named addressee you should not 
disseminate, distribute or copy this e-mail.

Reply via email to