Last updated: 2006-09-18

This website is based on NLog v1.0. Click here to view the documentation for other versions.

RichTextBox Target


Log text to Text property of RichTextBox of specified Name

AssemblyClass.NET Framework.NET CFMono on WindowsMono on Unix
1.01.12.01.02.01.02.01.02.0
NLog.dllNLog.Targets.RichTextBoxTarget      

Parameters (blue fields are required):

NameTypeDescription
controlNamestring
Name of RichTextBox to which Nlog will log
layoutstring  ${}
The text to be rendered.

Default value is: ${longdate}|${level:uppercase=true}|${logger}|${message}.

namestring
The name of the target.
formNamestring
Name of the Form on which the control is located. If there is no open form of a specified name than NLog will create a new one.
useDefaultRowColoringRulesboolean
Use default coloring rules

Default value is: False.

RowColoringRules Collection of RichTextBoxRowColoringRule. Each element is represented as <row-coloring/>
NameTypeDescription
conditionstring  [c()]
The condition that must be met in order to set the specified font color.
fontColorstring
The font color. Names are identical with KnownColor enum extended with Empty value which means that background color won't be changed

Default value is: Empty.

backgroundColorstring
The background color. Names are identical with KnownColor enum extended with Empty value which means that background color won't be changed Background color will be set only in .net 2.0

Default value is: Empty.

styleFontStyle
Font style of matched text. Possible values are the same as in FontStyle enum in System.Drawing
WordColoringRules Collection of RichTextBoxWordColoringRule. Each element is represented as <word-coloring/>
NameTypeDescription
regexstring
The regular expression to be matched. You must specify either text or regex.
textstring
The text to be matched. You must specify either text or regex.
styleFontStyle
Font style of matched text. Possible values are the same as in FontStyle enum in System.Drawing
wholeWordsboolean
Match whole words only.

Default value is: False.

ignoreCaseboolean
Ignore case when comparing texts.

Default value is: False.

fontColorstring
The font color. Names are identical with KnownColor enum extended with Empty value which means that font color won't be changed

Default value is: Empty.

backgroundColorstring
The background color. Names are identical with KnownColor enum extended with Empty value which means that background color won't be changed Background color will be set only in .net 2.0

Default value is: Empty.

Example:

To set up the target in the configuration file, use the following syntax:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="richTextBox" xsi:type="RichTextBox" controlName="richTextBox1" 
            formName="form1" useDefaultRowColoringRules="false"/> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="richTextBox"/> 
  </rules> 
</nlog> 

The result is:

To set up the target with coloring rules in the configuration file, use the following syntax:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="richTextBox" xsi:type="RichTextBox" controlName="richTextBox1" 
            formName="form1" useDefaultRowColoringRules="true"> 
      <row-coloring condition="contains(message,'serious')" fontColor="Red" 
                    backgroundColor="Blue" style="Underline,Italic"/> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="richTextBox"/> 
  </rules> 
</nlog> 
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="richTextBox" xsi:type="RichTextBox" controlName="richTextBox1" 
            formName="form1" useDefaultRowColoringRules="true"> 
      <word-coloring text="message" fontColor="Green" backgroundColor="Black"/> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="richTextBox"/> 
  </rules> 
</nlog> 

The result is:

To set up the log target programmatically similar to above use code like this:

using System; 
using System.Text; 
using System.Windows.Forms; 
using NLog; 
using NLog.Targets; 
 
namespace RichTextBox2 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
 
            RichTextBoxTarget target = new RichTextBoxTarget(); 
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}"; 
            target.ControlName = "richTextBox1"; 
            target.FormName = "Form1"; 
            target.UseDefaultRowColoringRules = true; 
 
            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); 
 
            Logger logger = LogManager.GetLogger("Example"); 
            logger.Trace("trace log message"); 
            logger.Debug("debug log message"); 
            logger.Info("info log message"); 
            logger.Warn("warn log message"); 
            logger.Error("error log message"); 
            logger.Fatal("fatal log message"); 
        } 
    } 
}
,
using System; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using NLog; 
using NLog.Targets; 
 
namespace RichTextBox2 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
 
            RichTextBoxTarget target = new RichTextBoxTarget(); 
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}"; 
            target.ControlName = "richTextBox1"; 
            target.FormName = "Form1"; 
            target.UseDefaultRowColoringRules = false; 
            target.RowColoringRules.Add( 
                    new RichTextBoxRowColoringRule( 
                        "level >= LogLevel.Error and contains(message,'serious')", // condition 
                        "White", // font color 
                        "Red", // background color 
                        FontStyle.Bold | FontStyle.Italic 
                        ) 
                    ); 
 
            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); 
 
            Logger logger = LogManager.GetLogger("Example"); 
            logger.Trace("trace log message"); 
            logger.Debug("debug log message"); 
            logger.Info("info log message"); 
            logger.Warn("warn log message"); 
            logger.Error("error log message"); 
            logger.Fatal("fatal log message"); 
            logger.Fatal("fatal log message, rather serious"); 
        } 
    } 
}
for RowColoring,
using System; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using NLog; 
using NLog.Targets; 
 
namespace RichTextBox2 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
 
            RichTextBoxTarget target = new RichTextBoxTarget(); 
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}"; 
            target.ControlName = "richTextBox1"; 
            target.FormName = "Form1"; 
            target.UseDefaultRowColoringRules = false; 
            target.WordColoringRules.Add( 
                    new RichTextBoxWordColoringRule( 
                        "log", // word 
                        "White", // font color 
                        "Red", // background color 
                        FontStyle.Bold | FontStyle.Italic 
                        ) 
                    ); 
 
            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); 
 
            Logger logger = LogManager.GetLogger("Example"); 
            logger.Trace("trace log message"); 
            logger.Debug("debug log message"); 
            logger.Info("info log message"); 
            logger.Warn("warn log message"); 
            logger.Error("error log message"); 
            logger.Fatal("fatal log message"); 
            logger.Fatal("fatal log message, rather serious"); 
        } 
    } 
}
for WordColoring

Back to the target list.
Webwww.nlog-project.org