C#log4net TextWriterAppender未使用TextBox
C#项目1:图书馆C#log4net TextWriterAppender未使用TextBox,c#,log4net,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) {
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;
// ...
}
}
结果显示在taskmdummy.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窗体文本框(跨线程)。此项目加载IPlannedTask的实现(请参见项目1)并调用其方法run():
这导致直接调用WriteLine只在textbox中产生一行(请参见项目1)。log.Info调用不起作用(甚至没有调用TextBoxWriter::WriteLine方法)
=>为什么log4net的TextWriterAppender与Console一起工作。Out但与TextBoxWriter不一起工作如果将此设置为True,是否会产生影响?如果在运行时对appender进行更改,则必须调用
appender.ActivateOptions()
,才能生效。如果将其设置为True,会有什么不同吗?如果在运行时对appender进行更改,则必须调用appender.ActivateOptions()
,才能使更改生效。
// 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);