Categories: .Net, Performance, Security Posted by mheydt on 7/16/2006 1:30 AM | Comments (0)
I was upgrading software for a client today so that it wouldperiodically take a snapshot of the CPU utilization so that we couldmonitor the workload.  With some google based research, I came up withthe following code to return the cpu utilization:

private static PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");


Unfortunately,when I deployed this to the servers (it runs as a service), the servicewould not start.  Puzzled for a while I took a look at the event logand found the following error:

Servicecannot be started. System.TypeInitializationException: The typeinitializer for 'TNSMI.ArchiveServer.WatcherServiceImpl' threw anexception. ---> System.UnauthorizedAccessException: Access to theregistry key 'Global' is denied.
   at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
   at Microsoft.Win32.RegistryKey.InternalGetValue(String name, Object defaultValue, Boolean doNotExpand, Boolean checkSecurity)
   at Microsoft.Win32.RegistryKey.GetValue(String name)
   at System.Diagnostics.PerformanceMonitor.GetData(String item)
   at System.Diagnostics.PerformanceCounterLib.GetPerformanceData(String item)
   at System.Diagnostics.PerformanceCounterLib.get_CategoryTable()
   at System.Diagnostics.PerformanceCounterLib.CounterExists(String category, String counter, Boolean& categoryExists)
   at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter)
   at System.Diagnostics.PerformanceCounter.Initialize()
   at System.Diagnostics.PerformanceCounter.....


Turnsout the service is running under a limited account that did not havepermissions to access the performance counters.  The solution after alittle trying was to make the user account a member of the "PerformanceMonitor Users" group.

Technorati Tags: ,