Click or drag to resize

PerformanceCounterTarget Class

Increments specified performance counter on each write.
Inheritance Hierarchy
SystemObject
  NLog.TargetsTarget
    NLog.TargetsPerformanceCounterTarget

Namespace:  NLog.Targets
Assembly:  NLog.PerformanceCounter (in NLog.PerformanceCounter.dll) Version: 5.2.0+68ed826d5d3b17619b9ab78bfa53db526cadd9d2
Syntax
public class PerformanceCounterTarget : Target, 
	IInstallable

The PerformanceCounterTarget type exposes the following members.

Constructors
  NameDescription
Public methodPerformanceCounterTarget
Initializes a new instance of the PerformanceCounterTarget class.
Public methodPerformanceCounterTarget(String)
Initializes a new instance of the PerformanceCounterTarget class.
Top
Properties
  NameDescription
Public propertyAutoCreate
Gets or sets a value indicating whether performance counter should be automatically created.
Public propertyCategoryName
Gets or sets the name of the performance counter category.
Public propertyCounterHelp
Gets or sets the counter help text.
Public propertyCounterName
Gets or sets the name of the performance counter.
Public propertyCounterType
Gets or sets the performance counter type.
Public propertyIncrementValue
The value by which to increment the counter.
Public propertyInstanceName
Gets or sets the performance counter instance name.
Protected propertyIsInitialized
Gets a value indicating whether the target has been initialized.
(Inherited from Target.)
Protected propertyLoggingConfiguration
Gets the logging configuration this target is part of.
(Inherited from Target.)
Public propertyName
Gets or sets the name of the target.
(Inherited from Target.)
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
(Overrides TargetCloseTarget.)
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
(Inherited from Target.)
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)
Increments the configured performance counter.
(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
TODO: 1. Unable to create a category allowing multiple counter instances (.Net 2.0 API only, probably) 2. Is there any way of adding new counters without deleting the whole category? 3. There should be some mechanism of resetting the counter (e.g every day starts from 0), or auto-switching to another counter instance (with dynamic creation of new instance). This could be done with layouts.
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="pc" xsi:type="PerfCounter" categoryName="My category" 
 6                counterName="My counter" counterType="NumberOfItems32" instanceName="myInstance" />
 7    </targets>
 8
 9    <rules>
10        <logger name="*" minlevel="Debug" writeTo="pc" />
11    </rules>
12</nlog>

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

C#
 1using System;
 2
 3using NLog;
 4using NLog.Targets;
 5using NLog.Win32.Targets;
 6using System.Diagnostics;
 7
 8class Example
 9{
10    static void Main(string[] args)
11    {
12        PerfCounterTarget target = new PerfCounterTarget();
13        target.AutoCreate = true;
14        target.CategoryName = "My category";
15        target.CounterName = "My counter";
16        target.CounterType = PerformanceCounterType.NumberOfItems32;
17        target.InstanceName = "My instance";
18
19        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
20
21        Logger logger = LogManager.GetLogger("Example");
22        logger.Debug("log message");
23    }
24}
See Also