我正在使用NLog,並且想要同時登錄到RichTextBox和File。我想以編程方式配置Logger,而不是使用xml配置文件。如何使用NLog登錄到多個目標?
以下代碼只記錄到最後一個目標(本例中爲File)。任何人都可以幫忙嗎?
RichTextBoxTarget t1 = new RichTextBoxTarget();
t1.Layout = "${date} ${message}";
t1.ControlName = "rtb_log";
t1.FormName = "MainForm";
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(t1, LogLevel.Debug);
FileTarget t2 = new FileTarget();
t2.Layout = "${date} ${level} ${message}";
t2.FileName = "${basedir}/Logs/today.log";
t2.KeepFileOpen = false;
t2.Encoding = "iso-8859-2";
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(t2, LogLevel.Trace);
Logger logger = LogManager.GetLogger("MyLogger");
這種方式仍然覆蓋規則,限制你到一個規則。 Jason的回答對我來說似乎更加正確。 – GraemeF 2015-04-21 15:41:37