繁体   English   中英

NLog同时到RichTextBox和File

[英]NLog to RichTextBox and File at the same time

我有一个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>
    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完成)。 因此,您不应该再次关闭窗口,而只是将其隐藏并随后取消隐藏(避免创建多个目标注册)。

您可以通过调用以下命令为NLog配置第二个目标

LogManager.Configuration.AddTarget(asyncWrapper);

详细信息: https : //github.com/nlog/NLog/wiki/Configuration-API

这是假设您已经在其他地方设置了文件目标。 设置文件目标后,您需要调用该代码。 不过,了解如何配置文件日志记录也可能会有所帮助。

编辑:这是一个相关的问题: 如何使用NLog登录到多个目标?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM