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

Writes log message to the Event Log.

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

Syntax

C#
public class EventLogTarget : TargetWithLayout, 
	IInstallable
Visual Basic (Declaration)
Public Class EventLogTarget _
	Inherits TargetWithLayout _
	Implements IInstallable
Visual Basic (Usage)
Dim instance As EventLogTarget

Examples

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

CopyXML
 1<?xml version="1.0" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4    <targets>
 5        <target name="eventlog" xsi:type="EventLog" layout="${logger}: ${message}" source="My Source" log="Application" />
 6    </targets>
 7
 8    <rules>
 9        <logger name="*" minlevel="Debug" writeTo="eventlog" />
10    </rules>
11</nlog>

This assumes just one target and a single rule. More configuration options are described here.

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

CopyC#
 1using NLog;
 2using NLog.Targets;
 3using NLog.Win32.Targets;
 4
 5class Example
 6{
 7    static void Main(string[] args)
 8    {
 9        EventLogTarget target = new EventLogTarget();
10        target.Source = "My Source";
11        target.Log = "Application";
12        target.MachineName = ".";
13        target.Layout = "${logger}: ${message}";
14
15        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
16
17        Logger logger = LogManager.GetLogger("Example");
18        logger.Debug("log message");
19    }
20}

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.TargetWithLayout
      NLog.Targets..::.EventLogTarget

See Also