title: PerformanceCounter not working on WindowsAzure Cloud
body: <p>I am using Visual Studio 2012 with Windows Azure Tools (October 2012).
  I have WebService which does several things with to instances on Windows Azure Cloud.
  This works fine.</p>
  
  <p>Now I want to add a Performance Counter which stores in a certain interval (2 seconds) the total Processor usage in a table storage. Therefore I add in my WebRole onStart() method my performance counter method in a new thread like this:</p>
  
  <pre><code>counter = new MyPerformanceCounter();
  Thread thread = new Thread(new ThreadStart(counter.run));
  thread.Start();
  Trace.WriteLine("Thread Started");
  
  return base.OnStart(); 
  </code></pre>
  
  <p>My Performance Counter looks like this:</p>
  
  <pre><code>public class MyPerformanceCounter
  {
      TableStorageHelper tableHelper;
      PerformanceCounter myCounter;
  
      public MyPerformanceCounter()
      {
          this.tableHelper = new TableStorageHelper();
      }
      public void run()
      {
          if (!PerformanceCounterCategory.Exists("Processor"))
          {
              Trace.WriteLine("Object Processor does not exist!");
              return;
          }
  
          if (!PerformanceCounterCategory.CounterExists(@"% Processor Time", "Processor"))
          {
              Trace.WriteLine(@"Counter % Processor Time does not exist!");
              return;
          }
  
          this.myCounter = new PerformanceCounter("Processor", @"% Processor Time", @"_Total");
  
          while (true)
          {
  
              try
              {
                  float value = this.myCounter.NextValue();
                  Trace.WriteLine(@"Current value of Processor, %Processor Time, _Total= " + value.ToString());
                  tableHelper.persist(value);
              }
  
              catch
              {
                  Trace.WriteLine(@"_Total instance does not exist!");
                  continue;
              }
              // grep all 2 seconds new data (if exists)
              Thread.Sleep(2000);
          }
      } 
  </code></pre>
  
  <p>When I run the WebService locally it works fine.
  But when I deploy it into my Cloud, I get the following error message "The server encountered an error while retrieving metrics (InternalError). Retry the operation."
  The deploy process didn't return any errors. Only in Azure admin WebGui this error is shown or when I want to use my WebService.</p>
  
  <p>EDIT: 
  I added to my role's csdef file: </p>
  
  <pre><code>&lt;Runtime executionContext="elevated"&gt;&lt;/Runtime&gt;
  </code></pre>
  
  <p>Now the Webservice starts, runs and sends response. The Perfomance Counter creates a table in table storage (so the thread in webrole onstart is executed). But no performance data is written into the table storage.</p>
  
  <p>EDIT2: 
  Here is the code for table storage:</p>
  
  <pre><code>using Microsoft.WindowsAzure;
  using Microsoft.WindowsAzure.Storage;
  using Microsoft.WindowsAzure.Storage.Table;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace WCFServiceWebRole1
  {
  public class TableStorageHelper
  {
      CloudTable table;
      public TableStorageHelper() { init(); }
  
      private void init()
      {
          CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
              CloudConfigurationManager.GetSetting("mytable"));
          // Create the table client.
          CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
  
          // Create the table if it doesn't exist.
          this.table = tableClient.GetTableReference("mytable");
          this.table.CreateIfNotExists();
      }
  
      public void persist(float ProcessorValue)
      {
          PerformanceEntity entity = new PerformanceEntity();
          entity.ProcessorValue = ProcessorValue.ToString();
  
          TableOperation insertOperation = TableOperation.Insert(entity);
  
          // Execute the insert operation.
          this.table.Execute(insertOperation);
  
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And persistence entity:</p>
  
  <pre><code>using Microsoft.WindowsAzure.ServiceRuntime;
  using Microsoft.WindowsAzure.Storage.Table;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace WCFServiceWebRole1
  {
      public class PerformanceEntity : TableEntity
      {
          public PerformanceEntity()
          {
              this.PartitionKey = RoleEnvironment.CurrentRoleInstance.Id;
              this.RowKey = DateTime.Now.ToString();
          }
  
          public string ProcessorValue { get; set; }
  
      }
  }
  </code></pre>
  
  <p>Does anyone know, why I cant run this on Azure Cloud?</p>