I chose the second option (as I am already using DOMConfigurator) and it
worked beautifully. I added your SetTextBox() and GetAppenders() methods to
my TextBoxAppender, so all the code has to do is call
TextBoxAppender.SetTextBox(myTextBox).

Thanks very much for your time.

-----Original Message-----
From: Nicko Cadell [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, October 20, 2004 2:06 PM
To: Log4NET User
Subject: RE: TextBoxAppender


Either your TextBoxAppender would have to lookup the TextBox to write to,
i.e. make the TextBox into a singleton that the appender can lookup by
referencing a static field. Or something else has to tell the appender about
the TextBox to use. 

This depends on how you want to configure log4net. If you use the
BasicConfigurator you can do something like:

log4net.Config.BasicConfigurator.Configure(new
TextBoxAppender(MyTextBox));

If you want to use the DOMConfigurator to do the configuration your appender
will need to have a default constructor so that the configurator can create
it. After log4net has loaded the configuration you can find your appender
and set the TextBox to use.

public static void SetTextBox(TextBox tb)
{
  foreach(log4net.Appender.IAppender appender in GetAppenders())
  {
    if (appender is TextBoxAppender)
    {
      ((TextBoxAppender)appender).TextBox = tb;
    }
  }
}

public static log4net.Appender.IAppender[] GetAppenders()
{
  ArrayList appenders = new ArrayList();

 
appenders.AddRange(((log4net.Repository.Hierarchy.Hierarchy)log4net.LogM
anager.GetLoggerRepository()).Root.Appenders);
  foreach(log4net.ILog log in log4net.LogManager.GetCurrentLoggers())
  {
 
appenders.AddRange(((log4net.Repository.Hierarchy.Logger)log.Logger).App
enders);
  }

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


Hope that helps,

Nicko

> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] 
> Sent: 19 October 2004 19:39
> To: [email protected]
> Subject: TextBoxAppender
> 
> I'd like to have log4net append messages to a TextBox
> control.  I have subclassed AppenderSkeleton into a 
> TextBoxAppender class, but don't know how to pass it an 
> instance of TextBox to append to.
> 
> I found this same question posted on the old sourceforge list
> with no responses, so I'm asking again. (If anyone has 
> examples of doing similar things with other controls I could 
> just work from there).  
> 
> Here is my simple TextBoxAppender class:
> 
> public class TextBoxAppender : AppenderSkeleton 
>         {
> 
>                 private System.Windows.Forms.TextBox _textBox;
> 
>                 public
> TextBoxAppender(System.Windows.Forms.TextBox TextBox) 
>                 { 
>                         _textBox = TextBox; 
>                 } 
> 
>                 protected override void Append
> (log4net.spi.LoggingEvent LoggingEvent) 
>                 { 
>                         
> _textBox.AppendText(LoggingEvent.RenderedMessage); 
>                 }
> 
>         }
> 
> 
> 

Reply via email to