简体   繁体   English

用于登录到文本框的log4net配置

[英]log4net configuration for logging to textbox

I'm having difficulties trying to understand the configurations for log4net formName and textBoxName elements. 我在尝试了解log4net formName和textBoxName元素的配置时遇到困难。 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: 使用SkeletonAppender时,您只需要找到一种找到文本框的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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