2010-09-14 65 views
2

如何在WPF應用程序中使用RichTextBox目標? 我不希望有一個單獨的窗口與日誌,我希望所有的日誌消息在位於WPF對話框中的richTextBox中輸出。如何在WPF應用程序中使用NLog的RichTextBox目標?

我試過在裏面用RichTextBox方塊使用WindowsFormsHost,但那對我不起作用:無論如何NLog打開了單獨的Windows窗體。

回答

-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); 
+5

這樣做不適用於WPF。 – 2011-10-10 00:15:06

2

的平均時間解決方法是使用可用here的3類,則遵循以下步驟:

  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); 
} 
+0

我創建了一個示例項目並將其附加到[ticket](https://nlog.codeplex.com/workitem/6272),供那些需要查看操作更改的人員使用。 稍作修改以適應我的口味;-) – 2014-09-18 19:12:19

相關問題