logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Morten Andersen <mor...@vianett.no>
Subject Re: how to set property for application level
Date Tue, 13 Jun 2006 07:31:25 GMT
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()
    {

    }
}
---------------------------------------------------------------------------------------

Mime
View raw message