the textbox resides in the main ui thread.  if you invoke it directly you'll get an illegal cross thread exception (2.0 is more strict about this than 1.1).  the delegate's used to cross the thread boundaries correctly.

Ron Grabowski wrote:
I'm not an expert WinForms programmer...why did you use a delegate
instead of calling AppendText directly on textbox:

// untested
public class TextBoxAppender : log4net.Appender.IAppender
{
    TextBox textbox;
    string name = null;
    log4net.Layout.ILayout layout = new log4net.Layout.SimpleLayout();

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public TextBoxAppender(TextBox textbox)
    {
        this.textbox = textbox;
    }

    public void DoAppend(log4net.Core.LoggingEvent loggingEvent)
    {
        StringBuilder buffer = new StringBuilder();
        using (StringWriter writer = new StringWriter(buffer))
        {
            layout.Format(writer, loggingEvent);
        }
        textbox.AppendText(buffer.ToString());
    }

    public void Close()
    {

    }
}

Is it becuase the Form that contains the TextBox is on its own thread
so the main GUI isn't blocked becuase of logging?

--- Morten Andersen <[EMAIL PROTECTED]> wrote:

  
tinhuty he wrote:
    
One more question: I have a windows form application and I want
      
some 
    
of the log entries automatically appear in some area of form(for 
example right bottom of the form). what is the best way to do it?

      
Just create your own appender. I use a very simple appender called 
TextBoxAppender.


    
---------------------------------------------------------------------------------------
  
public class TextBoxAppender : log4net.Appender.IAppender
{
    delegate void AppendTextDelegate(string text);
    AppendTextDelegate invokeDelegate;
    TextBox textbox;
    string name = null;
    log4net.Layout.ILayout layout = new
log4net.Layout.SimpleLayout();

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public TextBoxAppender(TextBox textbox)
    {
        invokeDelegate = new AppendTextDelegate(AppendText);
        this.textbox = textbox;
    }

    public void DoAppend(log4net.Core.LoggingEvent loggingEvent)
    {
        StringBuilder buffer = new StringBuilder();
        using (StringWriter writer = new StringWriter(buffer))
        {
            layout.Format(writer, loggingEvent);
        }
        AppendText(buffer.ToString());
    }

    void AppendText(string input)
    {
        if (textbox.InvokeRequired)
            textbox.Invoke(invokeDelegate, input);
        else
            textbox.AppendText(input);
    }

    public void Close()
    {

    }
}

    
---------------------------------------------------------------------------------------
  


  

Reply via email to