[This is preliminary documentation and is subject to change.]
Increments specified performance counter on each write.
Namespace:
NLog.TargetsAssembly: NLog (in NLog.dll) Version: 2.0.1.0
Syntax
C# |
---|
public class PerformanceCounterTarget : Target, IInstallable |
Visual Basic (Declaration) |
---|
Public Class PerformanceCounterTarget _ Inherits Target _ Implements IInstallable |
Visual Basic (Usage) |
---|
Dim instance As PerformanceCounterTarget |
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:

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>
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:

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}