Last updated: 2006-09-18

This website is based on NLog v1.0. Click here to view the documentation for other versions.

PerfCounter Target


Increments specified performance counter on each write.

AssemblyClass.NET Framework.NET CFMono on WindowsMono on Unix
1.01.12.01.02.01.02.01.02.0
NLog.dllNLog.Win32.Targets.PerfCounterTarget      

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.

Parameters (blue fields are required):

NameTypeDescription
categoryNamestring
Performance counter category.
counterNamestring
Name of the performance counter.
namestring
The name of the target.
autoCreateboolean
Whether performance counter should be automatically created.
counterTypePerformanceCounterType
Performance counter type.
instanceNamestring
Instance name.

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="pc" xsi:type="PerfCounter" categoryName="My category" 
            counterName="My counter" counterType="NumberOfItems32" 
            instanceName="myInstance"/> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="pc"/> 
  </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 System; 
 
using NLog; 
using NLog.Targets; 
using NLog.Win32.Targets; 
using System.Diagnostics; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        PerfCounterTarget target = new PerfCounterTarget(); 
        target.AutoCreate = true; 
        target.CategoryName = "My category"; 
        target.CounterName = "My counter"; 
        target.CounterType = PerformanceCounterType.NumberOfItems32; 
        target.InstanceName = "My instance"; 
 
        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); 
 
        Logger logger = LogManager.GetLogger("Example"); 
        logger.Debug("log message"); 
    } 
}

Back to the target list.
Webwww.nlog-project.org