NLog extensions to Display Windows Performance Counters and Update Windows Performance Counters
-
Install the package
Install-Package NLog.PerformanceCounter
or in your csproj:<PackageReference Include="NLog.PerformanceCounter" Version="5.*" />
-
Add to your nlog.config:
<extensions> <add assembly="NLog.PerformanceCounter"/> </extensions>
Alternative register from code using fluent configuration API:
LogManager.Setup().SetupExtensions(ext => { ext.RegisterTarget<NLog.Targets.PerformanceCounterTarget>(); ext.RegisterLayoutRenderer<NLog.LayoutRenderers.PerformanceCounterLayoutRenderer>(); });
Example of NLog.config
-file that displays Windows Performance Counter value:
<nlog>
<extensions>
<add assembly="NLog.PerformanceCounter"/>
</extensions>
<targets>
<target name="console" xsi:type="console" layout="${message}|Memory=${performancecounter:category=Process:counter=Working Set}" />
</targets>
<rules>
<logger minLevel="Info" writeTo="console" />
</rules>
</nlog>
Example of NLog.config
-file that updates Windows Performance Counter value:
<nlog>
<extensions>
<add assembly="NLog.PerformanceCounter"/>
</extensions>
<targets>
<target name="perf" xsi:type="PerfCounter" counterName="123" categoryName="xyz" />
</targets>
<rules>
<logger minLevel="Info" writeTo="perf" />
</rules>
</nlog>