Win2019 Server offers new performance counters that can help with performance issues on hosts with RDS (=Remote Desktop Services).
They have to be activated with a registry key before usage:
https://learn.microsoft.com/en-us/windows-server/remote/remote-desktop-services/rds-rdsh-performance-counters
After a system reboot, they are available.
In perfmon.msc they are visible and can be used.
But when I start using C# code to read these counters.
The PerfCounters are visible in C# too:
PerformanceCounterCategory[] categories = PerformanceCounterCategory.GetCategories();
foreach(var category in categories)
{
Console.WriteLine(category.CategoryName);
}
Then I start reading the performance counter:
PerformanceCounter performanceCounter = null;
bool continueRunning = true;
try
{
performanceCounter = new PerformanceCounter("User Input Delay per Session", "Max Input Delay", "Max");
Console.WriteLine("PerfCounter created");
}
catch(Exception ex)
{
Console.WriteLine(string.Format("ERROR: Could not create PerfCounter = '{0}'", ex.Message));
continueRunning = false;
}
while (continueRunning)
{
try
{
float perfValue = performanceCounter.NextValue();
Console.WriteLine(string.Format("NextValue = '{0}'", perfValue));
}
catch(Exception ex)
{
Console.WriteLine(string.Format("ERROR: Could not read NextValue '{0}'", ex.Message));
continueRunning = false;
}
System.Threading.Thread.Sleep(2000);
}
Now, I get an error in the program and two eventlog-entries:
Program output:
PerfCounter created
ERROR: Coud not read NextValue 'The Counter layout for the Category specified is invalid, a counter of the type: AverageCount64, AverageTimer32, CounterMultiTimer, CounterMultiTimerInverse, CounterMultiTimer100Ns, CounterMultiTimer100NsInverse, RawFraction, or SampleFraction has to be immediately followed by any of the base counter types: AverageBase, CounterMultiBase, RawBase or SampleBase.'
Program terminated
The eventlog entries:
- Event-ID 1003: The Extensible Counter DLL "C:\Windows\System32\perfts.dll" for the "LSM" service returned an incorrect object length. The sum of the 1 objectlengths returned did not match the size of the buffer returned.
- Event-ID 1017: Disabled performance counter data collection from the "LSM" service because the performance counter library for that service has generated one or more errors. The errors that forced this action have been written to the application event log. Correct the errors before enabling the performance counters for this service.
From now on, the performance counters have disappeared (also in perfmon.msc) and are not visible/usable anymore.
Additionally, (for me) they cannot be reactivated.
The performance counters have disappeared in perfmon.msc too.
What must be done to read these performance counters in C# without "loosing" them in the OS?
and also important
How can I make a lost performance counter visible/usable again?
You can save and restore the perfcounters with the Microsoft tool lodctr. So, if you already might have "lost" these counters, try a "lodctr /r" anyway. They might come back.
perfts.dll requires an 8 byte alignment which is not always fulfilled especially in .NET programs. A workaround that fixes this problem: Disable the checkbox "Prefer 32 bit" in VisualStudio 201. /ProjectSettings/Build/General