This blog post will detail the basics steps to log events occurring in a Windows Azure instance using the Windows Azure Diagnostics API. Like any production system, events such as errors or capacity shortfalls should be logged but abstracted from any normal user. An administrator should be able to get access to these logs, in order to trace any particular errors. Traditionally one may use Windows eventlogs, text files, database entries or other similar solutions to achieve this. Since Windows Azure is designed for internet scale applications, the solution for diagnostics needs to be able to capture multiple sources of information (say from each of 100 instances running a role) into a common place. This is where the Windows Azure Diagnostics API comes in.

In order to use this API, we first need to create a Windows Azure Project  in Visual Studio 2010. Use the File | New | Project command to achieve this:

This project will then be the basis of your application. Visual Studio will give you a prompt to ask you what type of projects you want to add into your Cloud Project and for our purposes we will create a Web Role as follows:

You may choose to create any kind of Role you wish in order to satisfy your application’s requirements. Since we are only aiming to provide a very basic logging functionality, we will simply provide a Web Form that gives a Textbox into which arbitrary data can be entered and a button to click that will then log that information.

If you were to use this application at the moment, nothing would happen. Your vanilla application has part of the setup that you require done for you, but not the entire amount. The project properties of your role provides a way to configure the diagnostics for you, but using this approach the data will never be transferred to Cloud Storage, as it is never configured to schedule a transfer for you. This means the logs will be written locally to each role, but never moved across to a place where you can access them centrally.

Still we will build on top of this, and simply add in the missing pieces of the logic. As a side note, you can do a lot of configuration using this approach. Full documentation can be found here .

The configuration that Visual Studio has added automatically for us is to add in a TraceListener for anything written using the System.Diagnostics.Trace class. The TraceListener implementation is Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener. At the moment we can say that our application is tracing, but tracing to a location local to each Role, and not being put anywhere useful for us to centrally download. In order to chance this, we need to use the DiagnosticMonitor class. This simple snippet of code shows how to achieve this:

public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
DiagnosticMonitorConfiguration diagnosticMonitorConfiguration = DiagnosticMonitor.GetDefaultInitialConfiguration();
diagnosticMonitorConfiguration.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
DiagnosticMonitor diagnosticMonitor = DiagnosticMonitor.Start(cloudStorageAccount, diagnosticMonitorConfiguration);
return base.OnStart();
}
}

This simple few lines of code go into the WebRole.cs class that Visual Studio 2010 created for you when you created your project. This tells the DiagnosticMonitor class to continuously transfer any logs written by a role to the correct location every 1 minute. Once this has been done, you can look in Azure TableStorage in order to find the entries added.

I used my simple application to log an Error, a Warning and a piece of Information.

Here are the results:

To get this ready for deployment to the real cloud (rather than DevFabric) you simply need to change the line which says:

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;

To access the actual storage account that you want to access to get at your data, something like:

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.FromConfigurationSetting("AzureFiddlerCore.ConnectionString");

I have attached the source code for this simple logging application here; WAD_Basic

I will post next on other types of Logging that can be enabled using the Windows Azure Diagnostics API.

Andy

UPDATE, I have been having problems getting this to run. In order to resolve this, I made a couple of changes. Firstly add this line to the start of your OnStart override in WebRole.cs. It ensures that the StorageAccount is available from config when you try to do so. I don’t have a good explanation as to why this started to go wrong after working firstly.

CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)));

Secondly, If you get a NullReferenceException when trying to Start the DiagnosticsManager, it could be that your connectionstring is invalid. Around the same time I started getting an error for the connectionstring not being available, I also started getting an error on DaignosticsManager.Start, and so I changed the Start method parameter to access the config itself:

DiagnosticMonitor diagnosticMonitor = DiagnosticMonitor.Start("AzureFiddlerCore.ConnectionString", diagnosticMonitorConfiguration);
About these ads