This project is read-only.
7

Closed

Add please to NLog WpfRichTextBoxTarget

description

I had written this feature himself, b/c it urgently needed to me.
But I think team of NLog can do this much better.
I attach sources of WpfRichTextBoxTarget, WpfRichTextBoxRowColoringRule and WpfRichTextBoxWordColoringRule.
The logic of WpfRichTextBoxWordColoringRule is not realized in WpfRichTextBoxTarget.
 
Thanks,
Alexandr.

file attachments

Closed Oct 7, 2012 at 6:45 PM by Xharze
Moved to https://github.com/NLog/NLog/issues/130

comments

ryker wrote Oct 6, 2011 at 6:24 PM

Hi,
Has anyone managed to use the code provided successfully and would be able to help on this?
Thanks,
Laurent

wrote Oct 7, 2011 at 11:59 AM

wrote Oct 10, 2011 at 1:11 AM

mayerwin wrote Oct 10, 2011 at 8:44 AM

I got it working by
  1. Importing the 3 files into my project,
  2. If not already the case, use Project > Add Reference to add references to the WPF assemblies: WindowsBase, PresentationCore, PresentationFramework.
  3. In WpfRichTextBoxTarget.cs, replace lines 188-203 with:
        //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. In your code, configure the new target like this for example:
       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);
    
I hope it helps, but it definitely does not seem to be a comprehensive implementation...

wrote Oct 7, 2012 at 6:45 PM

wrote Nov 12, 2012 at 7:58 PM

wrote Jan 14, 2013 at 8:54 AM

wrote Feb 22, 2013 at 1:12 AM

wrote Feb 22, 2013 at 7:36 AM

wrote May 16, 2013 at 12:34 PM

wrote Sep 18, 2014 at 8:05 PM

gonzalocontento wrote Sep 18, 2014 at 8:05 PM

I created a sample project for those who need to see the changes in action.
Minor changes to accommodate for my taste ;-)

gonzalocontento wrote Sep 18, 2014 at 8:07 PM

The sample file is WpfRichTextLogger.zip