Search code examples
azureperformancecounterazure-web-roles

Unable to create custom performance counters in Azure Web Role


When trying to create custom performance counters as explained in this MSDN article, I still get a security exception.

The code I am using is the same as in that article, only I am creating a different counters.

I understand that I can try to create it in a start-up task, but for now I want to do it inside the role.. it should work :/

The exception:

[SecurityException: Requested registry access is not allowed.]
   Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +12746267
   System.Diagnostics.PerformanceCounterLib.CreateRegistryEntry(String categoryName, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection creationData, Boolean& iniRegistered) +170
   System.Diagnostics.PerformanceCounterLib.RegisterCategory(String categoryName, PerformanceCounterCategoryType categoryType, String categoryHelp, CounterCreationDataCollection creationData) +49
   System.Diagnostics.PerformanceCounterCategory.Create(String categoryName, String categoryHelp, PerformanceCounterCategoryType categoryType, CounterCreationDataCollection counterData) +558

The beginning of my ServiceDefinition.csdef:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="Jonathan.Cloud" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebDriver" vmsize="Small" enableNativeCodeExecution="true">
    <Runtime executionContext="elevated" />

When I RDP into the role, I can see that the application pool is still running with the NetworkService identity, was expecting it to run under a different principal as I changed the role to be 'elevated' - manually setting up a custom principal with permissions solves this, but it will be over-ridden once the role reconfigure itself or if I deploy an upgrade - in any case, this is not really a solution but a hack :/ [frustrated]


Solution

  • In the case of Web Role of Full IIS, the special privilege which you appointed in Runtime element is applied to RoleEntoryPoint. I think that it is necessary to register a custom performance counter by inner RoleEntryPoint (usually WebRole.cs) OnStart method.