EventLog Target
Writes log message to the Event Log.
| Assembly | Class | .NET Framework | .NET CF | Mono on Windows | Mono on Unix |
|---|
| 1.0 | 1.1 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 |
|---|
| NLog.dll | NLog.Win32.Targets.EventLogTarget |  |  |  | | | | | | |
Parameters (blue fields are required):
| Name | Type | Description |
|---|
| layout | string
${} | | The text to be rendered. Default value is: ${longdate}|${level:uppercase=true}|${logger}|${message}. |
|
| name | string | |
| category | string
${} | | Layout that renders event Category. |
|
| eventID | string
${} | | Layout that renders event ID. |
|
| log | string | | Name of the Event Log to write to. This can be System, Application or any user-defined name. Default value is: Application. |
|
| machineName | string | | Machine name on which Event Log service is running. Default value is: .. |
|
| source | string | | The value to be used as the event Source. By default this is the friendly name of the current AppDomain. |
|
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="eventlog" xsi:type="EventLog" layout="${logger}: ${message}"
source="My Source" log="Application"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="eventlog"/>
</rules>
</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:
using NLog;
using NLog.Targets;
using NLog.Win32.Targets;
class Example
{
static void Main(string[] args)
{
EventLogTarget target = new EventLogTarget();
target.Source = "My Source";
target.Log = "Application";
target.MachineName = ".";
target.Layout = "${logger}: ${message}";
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message");
}
}
Back to the target list.