我有一个nlog配置文件设置为文件输出,此代码将输出设置为RichTextBox
public void SetupFormLogger()
{
NLog.Windows.Forms.RichTextBoxTarget target = new NLog.Windows.Forms.RichTextBoxTarget();
target.Name = "control";
target.Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}";
target.ControlName = richtextLog.Name;
target.FormName = this.Name;
target.TargetForm = this;
target.AutoScroll = true;
target.MaxLines = 10000;
target.UseDefaultRowColoringRules = false;
target.RowColoringRules.Add(
new RichTextBoxRowColoringRule(
"level == LogLevel.Trace", // condition
"WhiteSmoke", // font color
"Black", // background color
FontStyle.Regular
)
);
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Debug", "Gray", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Info", "WhiteSmoke", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Warn", "Yellow", "Black"));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Error", "White", "DarkRed", FontStyle.Bold));
target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Fatal", "Yellow", "DarkRed", FontStyle.Bold));
AsyncTargetWrapper asyncWrapper = new AsyncTargetWrapper();
asyncWrapper.Name = "AsyncRichTextBox";
asyncWrapper.WrappedTarget = target;
SimpleConfigurator.ConfigureForTargetLogging(asyncWrapper, LogLevel.Debug);
}
如果我调用此函数,它将不再记录到文件中,如果我不调用此函数,它将记录到文件。如何让NLog同时记录文件和RichTextBox?
这是我的nlog配置文件
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="myvar" value="myvalue"/>
<targets>
<target name="f" xsi:type="File" layout="${longdate} | ${module} | ${logger} | ${level} | ${message}" fileName="${basedir}/logs/${shortdate}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="f" />
</rules>
</nlog>
答案 0 :(得分:2)
LoggingRule richTextBoxRule = new LoggingRule("*", asyncWrapper);
richTextBoxRule.EnableLoggingForLevel(LogLevel.Debug);
richTextBoxRule.EnableLoggingForLevel(LogLevel.Error);
richTextBoxRule.EnableLoggingForLevel(LogLevel.Fatal);
richTextBoxRule.EnableLoggingForLevel(LogLevel.Info);
richTextBoxRule.EnableLoggingForLevel(LogLevel.Trace);
richTextBoxRule.EnableLoggingForLevel(LogLevel.Warn);
LogManager.Configuration.AddTarget(asyncWrapper.Name, asyncWrapper);
LogManager.Configuration.LoggingRules.Add(richTextBoxRule);
LogManager.ReconfigExistingLoggers();
可悲的是,再次动态关闭/删除目标并不容易(直到https://github.com/NLog/NLog/pull/2259完成)。所以你不应该再次关闭窗口,而是隐藏它然后取消隐藏(避免创建多个目标注册)。
答案 1 :(得分:1)
您可以通过调用
为NLog配置第二个目标LogManager.Configuration.AddTarget(asyncWrapper);
更多信息:https://github.com/nlog/NLog/wiki/Configuration-API
这假设您已经在其他地方设置了文件目标。您可能希望在设置文件目标后调用该代码。查看如何配置文件日志记录可能会有所帮助。
编辑:这是一个相关的问题:How to log to multiple targets using NLog?