| Name | Type | Description |
|---|
| controlName | string | | Name of RichTextBox to which Nlog will log |
|
| layout | string
${} | | The text to be rendered. Default value is: ${longdate}|${level:uppercase=true}|${logger}|${message}. |
|
| name | string | |
| formName | string | | 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. |
|
| useDefaultRowColoringRules | boolean | | Use default coloring rules Default value is: False. |
|
| RowColoringRules |
Collection of
RichTextBoxRowColoringRule. Each element is represented as <row-coloring/>
|
| Name | Type | Description |
|---|
| condition | string
[c()] | | The condition that must be met in order to set the specified font color. |
| | fontColor | string | | 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. |
| | backgroundColor | string | | 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. |
| | style | FontStyle | 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/>
|
| Name | Type | Description |
|---|
| regex | string | The regular expression to be matched. You must specify either text or regex. |
| | text | string | The text to be matched. You must specify either text or regex. |
| | style | FontStyle | Font style of matched text. Possible values are the same as in FontStyle enum in System.Drawing |
| | wholeWords | boolean | | Match whole words only. Default value is: False. |
| | ignoreCase | boolean | | Ignore case when comparing texts. Default value is: False. |
| | fontColor | string | | 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. |
| | backgroundColor | string | | 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. |
|
|
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