C#log4net TextWriterAppender无法使用TextBox

时间:2016-02-15 15:21:43

标签: c# log4net

C#项目1:图书馆

public class TaskDummy : IPlannedTask {

    private static readonly log4net.ILog log =
        log4net.LogManager.GetLogger(typeof(TaskDummy));

    // implements interface
    public void run(System.IO.TextWriter x, object paramIn=null) {
        initLog(x);
        String msg = "Dummy task is running";
        // direct & indirect call of TextWriter::WriteLine
        x.WriteLine(msg);
        log.Info(msg);
    }

    private void initLog(System.IO.TextWriter x) {
        log4net.Appender = new log4net.Appender.TextWriterAppender();
        textAppender.Writer = x;
        // ...
    }

}

导致TaskDummy.dll。 log4net在运行时配置了TextWriterAppender。

C#项目2:同一解决方案中的控制台应用程序,参考项目1。

class Program {
    static void Main(string[] args) {
        TaskDummy t = new TaskDummy();
        t.run(Console.Out);
    }
}

这将打印

    Dummy task is running
    INFO - Dummy task is running

如预期的那样(直接和间接呼叫)。 TextWriter是控制台输出中的一个。

C#项目3:Windows窗体

public class TextBoxWriter : TextWriter {

    protected TextBox box;

    public TextBoxWriter(TextBox box) {
        this.box = box; 
    }

    public override Encoding Encoding {
        get { return Encoding.Default; }
    }

    delegate void WriteLineCallBack(String value);

    public override void WriteLine(String value) {
        if(box.InvokeRequired) {
            WriteLineCallBack d = new WriteLineCallBack(WriteLine);
            box.Invoke(d, new object[] { value });
        } else {
            base.Write(value);
            box.AppendText(value.ToString() + Environment.NewLine);
        }
    }
}

实现TextWriter以写入Windows Forms Textbox(跨线程)。该项目加载IPlannedTask的实现(参见项目1)并调用它们的方法run():

// txtLog is a Textbox control
TextBoxWriter twOutput = new TextBowWriter(txtLog);

var DLL = Assembly.LoadFile("TaskDummy.dll");
Type t = DLL.getExportedTypes()[0];
dynamic d = Activator.CreateInstance(t);
((IPlannedTask)d).run(twOutput);

这导致WriteLine直接调用文本框中只有一行(参见项目1)。 log.Info调用不起作用(甚至不调用TextBoxWriter :: WriteLine方法)。

=>为什么log4net的TextWriterAppender与Console.Out一起使用但不与TextBoxWriter一起使用

0 个答案:

没有答案