Saturday, May 7, 2011

Initializing Windows Azure Diagnostic – Part 2

As you have read in my previous article how to initialize Windows Azure diagnostic monitor. Now we will see how to collect different diagnostic data. I am defining a connection string for the role. For that right click on the role and click on properties. Now from the connection tab you can add configuration string.
clip_image002

1) Collecting data from event logs
Open the Role entry point class. Add following code to the OnStart() function
var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
config.WindowsEventLog.DataSources.Add("System!*");
config.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Warning;
config.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
DiagnosticMonitor.Start("DiagnosticsConnectionString", config);

So, first you need to get DiagnosticMonitorConfiguration object. Then you can add channel to the datasource. You can add multiple channels. Windows Azure cannot log Security channel. You can define log level filter, so that only those logs are transferred. You have to define schedule transfer period, so that after that time span logs are transferred to the account you specified in the configuration. Now start the diagnostic monitor with the connection and new configuration.

2) Collect data from Performance Counters
Open the Role entry point class. Add following code to the OnStart() function

var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
config.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration()
{
CounterSpecifier = @"\Processor(_Total)\% Processor Time",
SampleRate = TimeSpan.FromMinutes(1.0),
});
config.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(5.0);
DiagnosticMonitor.Start("DiagnosticsConnectionString", config);

Here I have defined a performance counter which takes sample for processor time in every minute. You can add multiple performance counters in the data souce. And then transfer the log to azure account defined in the connection string after every 5 minutes.

3) Collect data from IIS logs
IIS logs are collected only for web role because worker and VM roles don’t run under IIS.
To enable IIS logging add following code to the web.config.

<tracing>
<traceFailedRequests>
<add path="*">
<traceAreas>
<add provider="ASP" verbosity="Verbose" />
<add provider="ASPNET" areas="Infrastructure, Module, Page, AppServices" verbosity="Verbose" />
<add provider="ISAPI Extension" verbosity="Verbose" />
<add provider="WWW Server"
areas="Authentication, Security, Filter, StaticFile, CGI, Compression, Cache, RequestNotifications, Module"
verbosity="Verbose" />
</traceAreas>
<failureDefinitions statusCodes="400-599" />
</add>
</traceFailedRequests>
</tracing>
After adding this code IIS logs are automatically logged. No additional API calls are required.

4) Collect data from crash dumps
In the role entry point class add the following code;

Microsoft.WindowsAzure.Diagnostics.CrashDumps.EnableCollection(true);

Pass true for enabling complete crash dump data, or false for partial crash dump data.

Initializing Windows Azure Diagnostics- Part 1

As we have seen in my previous blog that some of diagnostics data is stored in table, while some in blob. For collecting diagnostics data, we must initialize Windows Azure diagnostic monitor. Windows Azure diagnostic monitor runs in Windows Azure and in the compute emulator and collects diagnostic data for a role instance. Only Windows Azure Logs, Windows Azure Diagnostic Infrastructure Logs and IIS logs are configured in the diagnostic monitor by default. We can collect other diagnostic data by importing diagnostic module. A role instance that imports the Diagnostic module starts, it starts the diagnostic monitor.
To start diagnostic monitor for a role open the role entry point class and make sure that you are using Microsoft.WindowsAzure.Diagnostics namespace in the class. Now on the Azure service project right click on the role and click Properties. Here I am configuring a web role.
clip_image002
Now in the configuration section check enable diagnostics. And specify the account credentials
clip_image004
You can either use development storage or can give your account credentials. Just click on “…” and will ask for the credentials.
clip_image006
Now when you open your .cscfg file it has a new setting for the webrole like
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=myaccountName;AccountKey=myAccountKey" />
</ConfigurationSettings>

And in the .csdef file it import the module like:
<Imports>
<Import moduleName="Diagnostics" />
</Imports>

If you are using a web role or worker role template for creating roles then in the web.config or app.config following code is added. Or you can just inherit a class from RoleEntryPoint and add the following code in the config file.


<system.diagnostics>
<trace>
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
</trace>
</system.diagnostics>


 If you want to create a web role just override OnStart() method, and if you want to create a workerrole override OnStart() and Run() method. We will see it in my next article.