Thursday, December 8, 2011

Azure Diagnostics – Creating Windows Azure Diagnostics and Performance Counters Table

In Azure application development you may need to perform Windows Azure Logging. The best way to debug windows Azure application is to write diagnostics information in Azure Table Storage.
Also, System Center Operation Manager (SCOM) can monitor performance of Azure Application. A management pack has been already released for Azure Application monitoring using SCOM. In such scenario for monitoring Azure Performance counter, SCOM reads information from Azure Table Storage. For monitoring health information of Azure deployment from SCOM, diagnostics information is read from Azure Table Storage only.
But before using these tools for visualizing data one needs to capture diagnostics data; for which you will need to write some code in role “onstart” method.

Here I am presenting code which can be used for collecting Azure Role Instance event log information and transferring it to diagnostics table – WADWindowsEventLogsTable and performance counter information in WADPerformanceCountersTable.
Add reference to required Azure DLL’s and then use following code - 
public override bool OnStart()
    {
        var diagConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();

        var procTimeConfig = new PerformanceCounterConfiguration();
        procTimeConfig.CounterSpecifier = @"\Processor(*)\% Processor Time";
        procTimeConfig.SampleRate = System.TimeSpan.FromSeconds(5.0);
        diagConfig.PerformanceCounters.DataSources.Add(procTimeConfig);

        var diskBytesConfig = new PerformanceCounterConfiguration();
        diskBytesConfig.CounterSpecifier = @"\LogicalDisk(*)\Disk Bytes/sec";
        diskBytesConfig.SampleRate = System.TimeSpan.FromSeconds(5.0);
        diagConfig.PerformanceCounters.DataSources.Add(diskBytesConfig);

        var workingSetConfig = new PerformanceCounterConfiguration();
        workingSetConfig.CounterSpecifier =
            @"\Process(" +
            System.Diagnostics.Process.GetCurrentProcess().ProcessName +
            @")\Working Set";
        workingSetConfig.SampleRate = System.TimeSpan.FromSeconds(5.0);
        diagConfig.PerformanceCounters.DataSources.Add(workingSetConfig);
        diagConfig.PerformanceCounters.ScheduledTransferPeriod =
          TimeSpan.FromSeconds(30);

        diagConfig.WindowsEventLog.DataSources.Add("System!*");
        diagConfig.WindowsEventLog.DataSources.Add("Application!*");
        diagConfig.WindowsEventLog.ScheduledTransferPeriod =
          TimeSpan.FromSeconds(30);

        DiagnosticMonitor.Start(          "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString",
         
diagConfig);

        return base.OnStart();
    }
The yellow marked area above is the name of connection string key which you are using for storing Azure Storage Credentials where diagnostics and performance counter information will be collected.

Once your role’s status changes to Ready, you will see two tables created under storage account –
WADPerformanceCountersTable and WADWindowsEventLogsTable.

Hope this helps.
Cheers…

Happy Programming!!!

No comments:

Post a Comment