【问题标题】:How can I use NLog's RichTextBox Target in WPF application?如何在 WPF 应用程序中使用 NLog 的 RichTextBox 目标?
【发布时间】:2011-04-11 22:52:36
【问题描述】:

如何在 WPF 应用程序中使用 RichTextBox Target? 我不想有一个单独的日志窗口,我希望所有日志消息都输出到 WPF 对话框中的richTextBox 中。

我尝试使用带有 RichTextBox 框的 WindowsFormsHost,但这对我不起作用:NLog 还是打开了单独的 Windows 窗体。

【问题讨论】:

    标签: wpf richtextbox nlog


    【解决方案1】:

    同时,一种解决方法是使用 3 个可用的类 here,然后按照以下步骤操作:

    1. 将这 3 个文件导入到您的项目中

    2. 如果还没有,请使用 Project > Add Reference 添加对 WPF 程序集的引用:WindowsBase, PresentationCore, PresentationFramework

    3. WpfRichTextBoxTarget.cs 中,将第 188-203 行替换为:

          //this.TargetRichTextBox.Invoke(new DelSendTheMessageToRichTextBox(this.SendTheMessageToRichTextBox), new object[] { logMessage, matchingRule });
          if (System.Windows.Application.Current.Dispatcher.CheckAccess() == false) {
              System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => {
                  SendTheMessageToRichTextBox(logMessage, matchingRule);
              }));
          }
          else {
              SendTheMessageToRichTextBox(logMessage, matchingRule);
          }
      }
      
      private static Color GetColorFromString(string color, Brush defaultColor) {
          if (defaultColor == null) return Color.FromRgb(255, 255, 255); // This will set default background colour to white.
          if (color == "Empty") {
              return (Color)colorConverter.ConvertFrom(defaultColor);
          }
      
          return (Color)colorConverter.ConvertFromString(color);
      }
      
    4. 在您的代码中,配置新目标,如下例所示:

    我希望它有所帮助,但它似乎绝对不是一个全面的实现......

    public void loading() {
        var target = new WpfRichTextBoxTarget();
        target.Name = "console";
        target.Layout = "${longdate:useUTC=true}|${level:uppercase=true}|${logger}::${message}";
        target.ControlName = "rtbConsole"; // Name of the richtextbox control already on your window
        target.FormName = "MonitorWindow"; // Name of your window where there is the richtextbox, but it seems it will not really be taken into account, the application mainwindow will be used instead.
        target.AutoScroll = true;
        target.MaxLines = 100000;
        target.UseDefaultRowColoringRules = true;
        AsyncTargetWrapper asyncWrapper = new AsyncTargetWrapper();
        asyncWrapper.Name = "console";
        asyncWrapper.WrappedTarget = target;
        SimpleConfigurator.ConfigureForTargetLogging(asyncWrapper, LogLevel.Trace);
    }
    

    【讨论】:

    • 我创建了一个示例项目并将其附加到ticket,供那些需要查看实际变化的人使用。小改动以适应我的口味 ;-)
    【解决方案2】:

    如果您在配置文件中定义了 RichTextBoxTarget,则会自动创建一个新表单。这是因为 NLog 在您的(命名的)表单和控件创建之前初始化。即使您没有任何规则指向目标。也许有更好的解决方案,但我通过以编程方式创建目标来解决它:

    using NLog;
    //[...]
    RichTextBoxTarget target = new RichTextBoxTarget();
    target.Name = "RichTextBox";
    target.Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}";
    target.ControlName = "textbox1";
    target.FormName = "Form1";
    target.AutoScroll = true;
    target.MaxLines = 10000;
    target.UseDefaultRowColoringRules = false;
    target.RowColoringRules.Add(
        new RichTextBoxRowColoringRule(
            "level == LogLevel.Trace", // condition
            "DarkGray", // font color
            "Control", // background color
            FontStyle.Regular
        )
    );
    target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Debug", "Gray", "Control"));
    target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Info", "ControlText", "Control"));
    target.RowColoringRules.Add(new RichTextBoxRowColoringRule("level == LogLevel.Warn", "DarkRed", "Control"));
    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.Trace);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      相关资源
      最近更新 更多