A day with .Net

My day to day experince in .net

Remotely Change Diagnostic Configuration Azure

Posted by vivekcek on April 13, 2012

In normal scenarios we configure diagnostic in WebRoles OnStart() method.But if we want to change the default configuration remotely,i mean from out side windows azure

using a windows application.We can done this using azure SDK DLL’s.

In this post i will show you how to add a performance counter to a deployed and running web role through a windows application.

1. Create a simple azure web role project.In the WebRole.cs file add the below code in OnStart() method.This code will start diagnostic monitor with default configuration

 public override bool OnStart()
        {
            var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
            DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);
            return base.OnStart();
                       
        }

2. Build the project and it will be deployed to development fabric.Find the deployment-id from compute emulator.In our case it is “deployment16(14)”

3. Now create new windows form project and add reference to Azure SDK DLL’S from ref folder of 1.6 version.

Microsoft.WindowsAzure.ServiceRuntime.dll
Microsoft.WindowsAzure.Diagnostics.dll
Microsoft.WindowsAzure.StorageClient

4. Design a web from with a textbox and button.Enter the deploymentID in text box.

5. The full code of the form application is given below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.Diagnostics.Management;
namespace DiagnosticConfig
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConfigure_Click(object sender, EventArgs e)
        {
            DeploymentDiagnosticManager dmanger = new DeploymentDiagnosticManager("UseDevelopmentStorage=true",textBox1.Text);

            //Get the diagnostic mangers of all the instances of the role DignosticWeb
            var DiganosticMangerCollectionOfRoleInstances = dmanger.GetRoleInstanceDiagnosticManagersForRole("DignosticWeb");

            //Create a performance counter configuration that can be applied to all role instances.
            PerformanceCounterConfiguration ProcessorTime = new PerformanceCounterConfiguration() {

                CounterSpecifier = @"\Processor(_Total)\% Processor Time",
                SampleRate=TimeSpan.FromMilliseconds(5.0)
              };
            //Apply performance counter to all role instances

            foreach (var instnceDiagnosticAgent in DiganosticMangerCollectionOfRoleInstances)
            {
                DiagnosticMonitorConfiguration instanceConfig = instnceDiagnosticAgent.GetCurrentConfiguration();
                instanceConfig.PerformanceCounters.DataSources.Add(ProcessorTime);
                instanceConfig.PerformanceCounters.BufferQuotaInMB = 200;
                instanceConfig.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
                instnceDiagnosticAgent.SetCurrentConfiguration(instanceConfig);

            }
        }
    }
}


Advertisements

One Response to “Remotely Change Diagnostic Configuration Azure”

  1. Daniel said

    It looks like this code wont work anymore with the current Azure version 😦 ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s