[This is preliminary documentation and is subject to change.]

Log text a Rich Text Box control in an existing or new form.

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 2.0.1.0

Syntax

C#
public sealed class RichTextBoxTarget : TargetWithLayout
Visual Basic (Declaration)
Public NotInheritable Class RichTextBoxTarget _
	Inherits TargetWithLayout
Visual Basic (Usage)
Dim instance As RichTextBoxTarget

Examples

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

CopyXML
 1<?xml version="1.0" encoding="utf-8" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4
 5    <targets>
 6        <target name="richTextBox" xsi:type="RichTextBox" controlName="richTextBox1" formName="form1" useDefaultRowColoringRules="false"/>
 7    </targets>
 8
 9    <rules>
10        <logger name="*" minlevel="Debug" writeTo="richTextBox" />
11    </rules>
12</nlog>

The result is:

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

CopyXML
 1<?xml version="1.0" encoding="utf-8" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4
 5    <targets>
 6        <target name="richTextBox" xsi:type="RichTextBox" controlName="richTextBox1" formName="form1" useDefaultRowColoringRules="true">
 7            <row-coloring condition="contains(message,'serious')" fontColor="Red" backgroundColor="Blue" style="Underline,Italic" />
 8        </target>
 9    </targets>
10
11    <rules>
12        <logger name="*" minlevel="Debug" writeTo="richTextBox" />
13    </rules>
14</nlog>
CopyXML
 1<?xml version="1.0" encoding="utf-8" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4
 5    <targets>
 6        <target name="richTextBox" xsi:type="RichTextBox" controlName="richTextBox1" formName="form1" useDefaultRowColoringRules="true">
 7            <word-coloring text="message" fontColor="Green" backgroundColor="Black"/>
 8        </target>
 9    </targets>
10
11    <rules>
12        <logger name="*" minlevel="Debug" writeTo="richTextBox" />
13    </rules>
14</nlog>

The result is:

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

CopyC#
 1using System;
 2using System.Text;
 3using System.Windows.Forms;
 4using NLog;
 5using NLog.Targets;
 6
 7namespace RichTextBox2
 8{
 9    public partial class Form1 : Form
10    {
11        public Form1()
12        {
13            InitializeComponent();
14        }
15
16        private void Form1_Load(object sender, EventArgs e)
17        {
18
19            RichTextBoxTarget target = new RichTextBoxTarget();
20            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
21            target.ControlName = "richTextBox1";
22            target.FormName = "Form1";
23            target.UseDefaultRowColoringRules = true;
24
25            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
26
27            Logger logger = LogManager.GetLogger("Example");
28            logger.Trace("trace log message");
29            logger.Debug("debug log message");
30            logger.Info("info log message");
31            logger.Warn("warn log message");
32            logger.Error("error log message");
33            logger.Fatal("fatal log message");
34        }
35    }
36}
,
CopyC#
 1using System;
 2using System.Drawing;
 3using System.Text;
 4using System.Windows.Forms;
 5using NLog;
 6using NLog.Targets;
 7
 8namespace RichTextBox2
 9{
10    public partial class Form1 : Form
11    {
12        public Form1()
13        {
14            InitializeComponent();
15        }
16
17        private void Form1_Load(object sender, EventArgs e)
18        {
19
20            RichTextBoxTarget target = new RichTextBoxTarget();
21            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
22            target.ControlName = "richTextBox1";
23            target.FormName = "Form1";
24            target.UseDefaultRowColoringRules = false;
25            target.RowColoringRules.Add(
26                    new RichTextBoxRowColoringRule(
27                        "level >= LogLevel.Error and contains(message,'serious')", // condition
28                        "White", // font color
29                        "Red", // background color
30                        FontStyle.Bold | FontStyle.Italic
31                        )
32                    );
33
34            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
35
36            Logger logger = LogManager.GetLogger("Example");
37            logger.Trace("trace log message");
38            logger.Debug("debug log message");
39            logger.Info("info log message");
40            logger.Warn("warn log message");
41            logger.Error("error log message");
42            logger.Fatal("fatal log message");
43            logger.Fatal("fatal log message, rather serious");
44        }
45    }
46}
for RowColoring,
CopyC#
 1using System;
 2using System.Drawing;
 3using System.Text;
 4using System.Windows.Forms;
 5using NLog;
 6using NLog.Targets;
 7
 8namespace RichTextBox2
 9{
10    public partial class Form1 : Form
11    {
12        public Form1()
13        {
14            InitializeComponent();
15        }
16
17        private void Form1_Load(object sender, EventArgs e)
18        {
19
20            RichTextBoxTarget target = new RichTextBoxTarget();
21            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
22            target.ControlName = "richTextBox1";
23            target.FormName = "Form1";
24            target.UseDefaultRowColoringRules = false;
25            target.WordColoringRules.Add(
26                    new RichTextBoxWordColoringRule(
27                        "log", // word
28                        "White", // font color
29                        "Red", // background color
30                        FontStyle.Bold | FontStyle.Italic
31                        )
32                    );
33
34            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
35
36            Logger logger = LogManager.GetLogger("Example");
37            logger.Trace("trace log message");
38            logger.Debug("debug log message");
39            logger.Info("info log message");
40            logger.Warn("warn log message");
41            logger.Error("error log message");
42            logger.Fatal("fatal log message");
43            logger.Fatal("fatal log message, rather serious");
44        }
45    }
46}
for WordColoring

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.TargetWithLayout
      NLog.Targets..::.RichTextBoxTarget

See Also