如何在 WPF 应用程序中使用 RichTextBox Target?我不想有一个单独的日志窗口,我希望所有日志消息都输出到 WPF 对话框中的richTextBox 中。
我曾尝试在内部使用带有 RichTextBox 框的 WindowsFormsHost,但这对我不起作用:NLog 还是打开了单独的 Windows 窗体。
如何在 WPF 应用程序中使用 RichTextBox Target?我不想有一个单独的日志窗口,我希望所有日志消息都输出到 WPF 对话框中的richTextBox 中。
我曾尝试在内部使用带有 RichTextBox 框的 WindowsFormsHost,但这对我不起作用:NLog 还是打开了单独的 Windows 窗体。
同时,一种解决方法是使用此处提供的 3 个类,然后按照以下步骤操作:
将 3 个文件导入到您的项目中
如果还没有,请使用Project > Add Reference
添加对 WPF 程序集的引用: WindowsBase, PresentationCore, PresentationFramework
.
在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);
}
在您的代码中,配置新目标,如下例所示:
我希望它有所帮助,但它绝对不是一个全面的实现......
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);
}
如果您在配置文件中定义 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);