PerformanceCounter无法使用WindowsAzure Cloud

时间:2013-01-01 11:11:39

标签: c# azure performancecounter azure-web-roles

我正在使用Visual Studio 2012和Windows Azure Tools(2012年10月)。 我有WebService,可以对Windows Azure Cloud上的实例执行多项操作。 这很好。

现在我想添加一个性能计数器,它以一定的间隔(2秒)存储表存储中的总处理器使用量。因此,我在我的WebRole onStart()方法中添加了我的性能计数器方法,如下所示:

counter = new MyPerformanceCounter();
Thread thread = new Thread(new ThreadStart(counter.run));
thread.Start();
Trace.WriteLine("Thread Started");

return base.OnStart(); 

我的表现计数器看起来像这样:

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);
        }
    } 

当我在本地运行WebService时,它工作正常。 但是当我将其部署到我的云中时,我收到以下错误消息“服务器在检索指标时遇到错误(InternalError)。重试该操作。” 部署过程未返回任何错误。仅在Azure管理员WebGui中显示此错误或当我想使用我的WebService时。

编辑: 我添加到我的角色的csdef文件中:

<Runtime executionContext="elevated"></Runtime>

现在Webservice启动,运行并发送响应。 Perfomance Counter在表存储中创建一个表(因此执行webrole onstart中的线程)。但是没有性能数据写入表存储。

EDIT2: 以下是表存储的代码:

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);

    }

}

}

和持久性实体:

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; }

    }
}

有谁知道,为什么我不能在Azure云上运行它?

0 个答案:

没有答案