Click or drag to resize

EventLogTarget Class

Writes log message to the Event Log.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsTargetWithLayout
      NLog.TargetsEventLogTarget

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 5.3.4+73d83d3161d31288ca5c554cc50d27b6bed5f28b
Syntax
public class EventLogTarget : TargetWithLayout, 
	IInstallable

The EventLogTarget type exposes the following members.

Constructors
  NameDescription
Public methodEventLogTarget
Initializes a new instance of the EventLogTarget class.
Public methodEventLogTarget(String)
Initializes a new instance of the EventLogTarget class.
Top
Properties
  NameDescription
Public propertyCategory
Gets or sets the layout that renders event Category.
Public propertyEntryType
Optional entry type. When not set, or when not convertible to EventLogEntryType then determined by LogLevel
Public propertyEventId
Gets or sets the layout that renders event ID.
Protected propertyIsInitialized
Gets a value indicating whether the target has been initialized.
(Inherited from Target.)
Public propertyLayout
Gets or sets the layout used to format log messages.
(Inherited from TargetWithLayout.)
Public propertyLog
Gets or sets the name of the Event Log to write to. This can be System, Application or any user-defined name.
Protected propertyLoggingConfiguration
Gets the logging configuration this target is part of.
(Inherited from Target.)
Public propertyMachineName
Gets or sets the name of the machine on which Event Log service is running.
Public propertyMaxKilobytes
Gets or sets the maximum Event log size in kilobytes.
Public propertyMaxMessageLength
Gets or sets the message length limit to write to the Event Log.
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
Public propertyOnOverflow
Gets or sets the action to take if the message is larger than the MaxMessageLength option.
Public propertySource
Gets or sets the value to be used as the event Source.
Protected propertySyncRoot
Gets the object which can be used to synchronize asynchronous operations that must rely on the .
(Inherited from Target.)
Top
Methods
  NameDescription
Protected methodCloseTarget
Closes the target to release any initialized resources
(Inherited from Target.)
Public methodDispose
Closes the target.
(Inherited from Target.)
Protected methodDispose(Boolean)
Releases unmanaged and - optionally - managed resources.
(Inherited from Target.)
Public methodFlush
Flush any pending log messages (in case of asynchronous targets).
(Inherited from Target.)
Protected methodFlushAsync
Flush any pending log messages
(Inherited from Target.)
Protected methodInitializeTarget
Initializes the target before writing starts
(Overrides TargetInitializeTarget.)
Public methodInstall
Performs installation which requires administrative permissions.
Public methodIsInstalled
Determines whether the item is installed.
Public methodPrecalculateVolatileLayouts
Calls the Precalculate(LogEventInfo) on each volatile layout used by this target. This method won't prerender if all layouts in this target are thread-agnostic.
(Inherited from Target.)
Protected methodRenderLogEvent(Layout, LogEventInfo)
Renders the logevent into a string-result using the provided layout
(Inherited from Target.)
Protected methodRenderLogEventT(LayoutT, LogEventInfo, T)
Renders the logevent into a result-value by using the provided layout
(Inherited from Target.)
Protected methodResolveServiceT
Resolve from DI ServiceRepository
(Inherited from Target.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Target.)
Public methodUninstall
Performs uninstallation which requires administrative permissions.
Protected methodWrite(AsyncLogEventInfo)
Writes async log event to the log target.
(Inherited from Target.)
Protected methodWrite(IListAsyncLogEventInfo)
Writes an array of logging events to the log target. By default it iterates on all events and passes them to "Write" method. Inheriting classes can use this method to optimize batch writes.
(Inherited from Target.)
Protected methodWrite(LogEventInfo)
Writes logging event to the target destination
(Overrides TargetWrite(LogEventInfo).)
Public methodWriteAsyncLogEvent
Writes the log to the target.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(AsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Public methodWriteAsyncLogEvents(IListAsyncLogEventInfo)
Writes the array of log events.
(Inherited from Target.)
Protected methodWriteAsyncThreadSafe(AsyncLogEventInfo)
Writes a log event to the log target, in a thread safe manner. Any override of this method has to provide their own synchronization mechanism. !WARNING! Custom targets should only override this method if able to provide their own synchronization mechanism. Layout-objects are not guaranteed to be thread-safe, so using them without a SyncRoot-object can be dangerous.
(Inherited from Target.)
Protected methodWriteAsyncThreadSafe(IListAsyncLogEventInfo)
Writes an array of logging events to the log target, in a thread safe manner. Any override of this method has to provide their own synchronization mechanism. !WARNING! Custom targets should only override this method if able to provide their own synchronization mechanism. Layout-objects are not guaranteed to be thread-safe, so using them without a SyncRoot-object can be dangerous.
(Inherited from Target.)
Protected methodWriteFailedNotInitialized
LogEvent is written to target, but target failed to successfully initialize
(Inherited from Target.)
Top
Remarks
Examples

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

XML
 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" onOverflow="Truncate" />
 6    </targets>
 7
 8    <rules>
 9        <logger name="*" minlevel="Debug" writeTo="eventlog" />
10    </rules>
11</nlog>

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

C#
 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        target.OnOverflow = EventLogTargetOverflowAction.Truncate;
15
16        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
17
18        Logger logger = LogManager.GetLogger("Example");
19        logger.Debug("log message");
20    }
21}
See Also