简体   繁体   中英

log4net configuration for logging to textbox

I'm having difficulties trying to understand the configurations for log4net formName and textBoxName elements. I have been following an earlier question here

specifically I am trying to create a custom user control containing a textbox to act as the logger

namespace foo {
    public partial class LoggingControl : UserControl, IAppender {
        private ILog _logger = LogManager.GetLogger(typeof(LoggingControl));

        public LoggingControl() {
            InitializeComponent();
        }

        public ILog Logger {
            get {
                return _logger;
            }
        }

        public string FormName {
            get;
            set;
        }

        public string TextBoxName {
            get;
           set;
        }

        public void DoAppend(log4net.Core.LoggingEvent loggingEvent) {
            textBox.AppendText(loggingEvent.MessageObject.ToString() + Environment.NewLine);
        }

        public void Close() {
        }
    }
}

in my config I have

<log4net>
    <appender name="Logger" type="foo.LoggingControl">
        <formName value="MainForm"/>    --- ###01
        <textBoxName value="textBox"/>  --- ###02
        <root>
            <level value="DEBUG">
                <appender-ref ref="textbox">
                </appender-ref>
            </level>
         </root>
    </appender>
</log4net>
  1. is set to the name of the form that contains the custom control which contains the logging textbox
  2. is set to the name of the textbox inside the custom control.

This isn't working, what am I missing?

When using a SkeletonAppender you only have to find a way to find the textbox:

public class TextBoxAppender : AppenderSkeleton
{
    protected override void Append(LoggingEvent loggingEvent)
    {
         TextBox tb = FindMyTextBox();
         tb.Text += RenderLoggingEvent(loggingEvent);
    }

    private TextBox FindMyTextBox(){
        //Get the textbox from your program and return it for logging
    }
}

An other option can be to expose a static logging text with property changed events and bind it to your textbox its text.

您应该使用textBox.BeginInvoke而不是textBox.Invoke来实现threadsafe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM