如何登录到多个目标使用NLOG? [英] How to log to multiple targets using NLog?
本文介绍了如何登录到多个目标使用NLOG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用NLOG,我想登录到RichTextBox中,并在同一时间文件。我想以编程方式配置的记录器,不使用XML配置文件。
下面的代码只记录到最后一个目标(在本例中的文件)。 ?任何人可以帮助
RichTextBoxTarget T1 =新RichTextBoxTarget();
t1.Layout =$ {日期} $ {}的消息;
t1.ControlName =rtb_log;
t1.FormName =MainForm的;
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(T1,LogLevel.Debug);
FileTarget T2 =新FileTarget();
t2.Layout =$ {日期} $ {级} $ {}的消息;
t2.FileName =$ {} BASEDIR /Logs/today.log
t2.KeepFileOpen = FALSE;
t2.Encoding =ISO-8859-2;
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(T2,LogLevel.Trace);
记录器记录= LogManager.GetLogger(MyLogger);
解决方案
好吧我知道了。我应该张贴问题之前,先阅读帮助文件。但不管怎样,答案是使用 SplitTarget
如下...
RichTextBoxTarget T1 =新RichTextBoxTarget();
t1.Layout =$ {日期} $ {}的消息;
t1.ControlName =rtb_log;
t1.FormName =MainForm的;
FileTarget T2 =新FileTarget();
t2.Layout =$ {日期} $ {级} $ {}的消息;
t2.FileName =$ {} BASEDIR /Logs/today.log
t2.KeepFileOpen = FALSE;
t2.Encoding =ISO-8859-2;
SplitTarget目标=新SplitTarget();
target.Targets.Add(T1);
target.Targets.Add(T2);
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(目标,LogLevel.Debug);
记录器记录= LogManager.GetLogger(MyLogger);
I am using NLog and I want to log to RichTextBox and File at the same time. And I want to configure the Logger programmatically, not with xml config file.
The following code only logs to the last target (File in this case). Can anybody help?
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");
解决方案
Okay I got it. I should've read the help file more before posting the question. But anyways, the answer is to use SplitTarget
as follows...
RichTextBoxTarget t1 = new RichTextBoxTarget();
t1.Layout = "${date} ${message}";
t1.ControlName = "rtb_log";
t1.FormName = "MainForm";
FileTarget t2 = new FileTarget();
t2.Layout = "${date} ${level} ${message}";
t2.FileName = "${basedir}/Logs/today.log";
t2.KeepFileOpen = false;
t2.Encoding = "iso-8859-2";
SplitTarget target = new SplitTarget();
target.Targets.Add(t1);
target.Targets.Add(t2);
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
Logger logger = LogManager.GetLogger("MyLogger");
这篇关于如何登录到多个目标使用NLOG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文