通过Xml文件配置log4net TextBoxAppender(自定义appender)

本文关键字:自定义 appender TextBoxAppender log4net Xml 文件 配置 通过 | 更新日期: 2023-09-27 17:50:14

这是我的问题的后续:灵活的日志接口…

我现在想为我的WinForms 2.0应用程序编写一个多行文本框的自定义log4net追加程序。StackOverflow的一个成员devdigital已经给我指出了这个链接:

文本框Appender

但是,本文没有描述如何通过Xml文件配置这样的appender。配置这个appender的唯一问题是,我们需要将对TextBox对象的引用传递给这个appender。

那么是否有可能使用Xml文件配置它?或者这样的appender只能通过编程方式配置吗?有哪些选项可以使其尽可能地可配置或松散耦合(可能是使用Xml文件和代码的组合)?

谢谢。

通过Xml文件配置log4net TextBoxAppender(自定义appender)

这取决于您如何配置log4net,但通常在log4net读取配置时不会创建表单(因此也不会创建textbox)。因此,您需要为表单和文本框名称创建属性。在附加日志事件之前,您应该检查表单是否打开并提供了文本框。此外,从AppenderSkeleton继承比从头开始实现IAppender更好:

public class TextBoxAppender : AppenderSkeleton
{
    private TextBox _textBox;
    public string FormName { get; set; }
    public string TextBoxName { get; set; }
    protected override void Append(LoggingEvent loggingEvent)
    {
        if (_textBox == null)
        {
            if (String.IsNullOrEmpty(FormName) || 
                String.IsNullOrEmpty(TextBoxName))
                return;
            Form form = Application.OpenForms[FormName];
            if (form == null)
                return;
            _textBox = form.Controls[TextBoxName] as TextBox;
            if (_textBox == null)
                return;
            form.FormClosing += (s, e) => _textBox = null;
        }
        _textBox.AppendText(loggingEvent.RenderedMessage + Environment.NewLine);
    }
}

配置很简单(log4net将读取xml元素并为具有相同名称的属性提供值):

<appender name="textbox" type="Foo.TextBoxAppender, Foo">
  <formName value="Form1"/>
  <textBoxName value="textBox1"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %logger - %message" />
  </layout>      
</appender>
<root>
  <level value="INFO" />
  <appender-ref ref="textbox"/>
</root>

我没有提供任何错误处理代码或多线程和线程同步相关的代码,因为问题是关于appender配置的。

这是所有上面注释的更新版本:线程安全,不锁定应用程序,并使用转换模式:

namespace MyNamespace
{
    public class TextBoxAppender : AppenderSkeleton
    {
        private TextBox _textBox;
        public TextBox AppenderTextBox
        {
            get
            {
                return _textBox;
            }
            set
            {
                _textBox = value;
            }
        }
        public string FormName { get; set; }
        public string TextBoxName { get; set; }
        private Control FindControlRecursive(Control root, string textBoxName)
        {
            if (root.Name == textBoxName) return root;
            foreach (Control c in root.Controls)
            {
                Control t = FindControlRecursive(c, textBoxName);
                if (t != null) return t;
            }
            return null;
        }
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            if (_textBox == null)
            {
                if (String.IsNullOrEmpty(FormName) ||
                    String.IsNullOrEmpty(TextBoxName))
                    return;
                Form form = Application.OpenForms[FormName];
                if (form == null)
                    return;
                _textBox = (TextBox)FindControlRecursive(form, TextBoxName);
                if (_textBox == null)
                    return;
                form.FormClosing += (s, e) => _textBox = null;
            }
            _textBox.BeginInvoke((MethodInvoker)delegate
            {
                _textBox.AppendText(RenderLoggingEvent(loggingEvent));
            });
        }
    }
}

配置,放在app.config:

<appender name="textboxAppender" type="MyNamespace.TextBoxAppender, MyNamespace">
  <formName value="MainForm"/>
  <textBoxName value="textBoxLog"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingFileAppender" />
  <appender-ref ref="textboxAppender" />
</root>   

我修改了appender以使用多线程。此外,我还附加了代码配置。

问候,bruno Dorin

Appender:

public class TextBoxAppender : AppenderSkeleton
{
    private TextBox _textBox;
    public TextBox AppenderTextBox
    {
        get
        {
            return _textBox;
        }
        set
        {
            _textBox = value;
        }
    }
    public string FormName { get; set; }
    public string TextBoxName { get; set; }
    private Control FindControlRecursive(Control root, string textBoxName)
    {
        if (root.Name == textBoxName) return root;
        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, textBoxName);
            if (t != null) return t;
        }
        return null;
    }
    protected override void Append(log4net.Core.LoggingEvent loggingEvent)
    {
        if (_textBox == null)
        {
            if (String.IsNullOrEmpty(FormName) ||
                String.IsNullOrEmpty(TextBoxName))
                return;
            Form form = Application.OpenForms[FormName];
            if (form == null)
                return;
            _textBox = (TextBox)FindControlRecursive(form, TextBoxName);
            if (_textBox == null)
                return;
            form.FormClosing += (s, e) => _textBox = null;
        }
        _textBox.Invoke((MethodInvoker)delegate
        {
            _textBox.AppendText(loggingEvent.RenderedMessage + Environment.NewLine);
        });
    }
}
配置:

           var textBoxAppender = new Util.TextBoxAppender();
        textBoxAppender.TextBoxName = "textLog";
        textBoxAppender.FormName = "MainTarget";
        textBoxAppender.Threshold = log4net.Core.Level.All;
        var consoleAppender = new log4net.Appender.ConsoleAppender { Layout = new log4net.Layout.SimpleLayout() };
        var list = new AppenderSkeleton[] { textBoxAppender, consoleAppender };
        log4net.Config.BasicConfigurator.Configure(list);

实际附加到文本框的行应该是…

_textBox.AppendText(RenderLoggingEvent(loggingEvent));

…如果你想利用模式布局。否则,它只发送消息的文本(默认布局)。

以上Klodoma的样品相当好。如果将文本框更改为richtextbox,则可以对输出执行更多操作。下面是一些按级别为消息颜色的代码:

        System.Drawing.Color text_color;
        switch (loggingEvent.Level.DisplayName.ToUpper())
        {
            case "FATAL":
                text_color = System.Drawing.Color.DarkRed;
                break;
            case "ERROR":
                text_color = System.Drawing.Color.Red;
                break;
            case "WARN":
                text_color = System.Drawing.Color.DarkOrange;
                break;
            case "INFO":
                text_color = System.Drawing.Color.Teal;
                break;
            case "DEBUG":
                text_color = System.Drawing.Color.Green;
                break;
            default:
                text_color = System.Drawing.Color.Black;
                break;
        }
        _TextBox.BeginInvoke((MethodInvoker)delegate
        {
            _TextBox.SelectionColor = text_color;
            _TextBox.AppendText(RenderLoggingEvent(loggingEvent));
        });

如果您真的想要,可以从log4net配置中以与ColorConsoleAppender相同的方式映射颜色,但我把它留给下一个编码员来偶然发现这个示例…

以下是klodoma的WPF/XAML版本的答案

  public class TextBoxAppender : AppenderSkeleton {
    private TextBox AppenderTextBox { get; set; }
    private Window window;
    public string WindowName { get; set; }
    public string TextBoxName { get; set; }
    private T FindControl<T>(Control root, string textBoxName) where T:class{
        if (root.Name == textBoxName) {
            return root as T;
        }
        return root.FindName(textBoxName) as T;
    }
    protected override void Append(log4net.Core.LoggingEvent loggingEvent) {
        if (window == null || AppenderTextBox == null) {
            if (string.IsNullOrEmpty(WindowName) ||
                string.IsNullOrEmpty(TextBoxName))
                return;
            foreach (Window window in Application.Current.Windows) {
                if (window.Name == WindowName) {
                    this.window = window;
                }
            }
            if (window == null)
                return;
            AppenderTextBox = FindControl<TextBox>(window, TextBoxName);
            if (AppenderTextBox == null)
                return;
            window.Closing += (s, e) => AppenderTextBox = null;
        }
        window.Dispatcher.BeginInvoke( new Action(delegate {
            AppenderTextBox.AppendText(RenderLoggingEvent(loggingEvent));
        }));
    }

和日志配置

 <appender name="textboxAppender" type="Namespace.TextBoxAppender, Namespace">
<windowName value="Viewer"/>
<textBoxName value="LogBox"/>
<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>

别忘了给你的窗口起一个名字(必须和窗口类型的名字不同)

如果您想在您的文件的多个位置进行日志记录,我更喜欢下面的方法应用程序。这种方法提供了动态更改控制实例的灵活性通过代码。

TextBoxAppender

public class TextBoxAppender : AppenderSkeleton
    {
        public RichTextBox RichTextBox { get; set; }
        protected override void Append(LoggingEvent loggingEvent)
        {
            Action operation = () => { this.RichTextBox.AppendText(RenderLoggingEvent(loggingEvent)); };
            this.RichTextBox.Invoke(operation);
        }
    }

分配文本框实例的代码。在启动执行日志记录的进程之前执行此操作。

 var appender = LogManager.GetRepository().GetAppenders().Where(a => a.Name == "TextBoxAppender").FirstOrDefault();
 if (appender != null)
       ((TextBoxAppender)appender).RichTextBox = this.richTextBoxLog;

配置
<log4net debug="false">
    <appender name="TextBoxAppender" type="SecurityAudit.UI.TextBoxAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <priority value="DEBUG" />
      <appender-ref ref="TextBoxAppender" />
    </root>
  </log4net>