This post is compatible with Windows Azure SDK v1.3 and v1.4

If you are planning to migrate an existing ASP.NET Web Application to Windows Azure, being able to diagnose any issues is a top priority. Windows Azure provides a comprehensive diagnostics system, designed to solve specific cloud computing diagnosis issues such as reporting statistics and errors across multiple instances in a common aggregated way. To take advantage of these diagnosis tools, you must undertake a few steps in your existing application in order to enable them. This post will detail those steps and the basics of how to migrate any Web Application to Azure.

The builds upon my implementation guide and source code is provided at the end of the post.
This post uses a vanilla ASP.NET web application, and details every step needed to make it equivalent to a brand new Azure Web Role in terms of its ability to use Windows Azure Diagnostics. I created the Web Application using File | New | Web Application – yours will be much more complex, but as a ground up approach, this is my vanilla application:

Web Application for Migration

Web Application for Migration

Next we have to create a Windows Azure Project.

Create a new Windows Azure Project

Create a new Windows Azure Project

Again this is a vanilla Project. Since we already have our source code in the project that we want to migrate, we don’t need to add any Roles to the solution. So click OK without adding any roles as below:

Create an Empty Azure Project

Create an Empty Azure Project

This empty Azure project is now ready for us to add our Web Application to. For reference this is the default empty project’s structure:

Empty Azure project

Empty Azure project

Now add the existing Web Application by right clicking on the solution and adding an Existing project, browsing to your project and adding it to the solution:

Add Existing Project

Add Existing Project

Browse to the solution

Browse to the solution

Add the project to the solution

Add the project to the solution

At this point you’ll notice that there are no Roles in your solution – this means there’s nothing for Azure to do, a Role is a project that will run on Azure. We must tell Visual Studio that the application that we just added is a Role, and to do that we right click on “Roles” and select a “Web Role in Solution”. This allows us to select from the project list that comes up – note that if you can’t see your project in the list, then you may need to migrate the project to being a Web Application first.

Here are the steps you need:

Add Web Role Project in Solution

Add Web Role Project in Solution

Select a Project

Select a Project

Your Role added

Your Role added

Now if we start this project with Visual Studio, we’ll see the application run. However, we will not see any diagnostics within the Windows Azure Diagnostics framework, as we haven’t configured anything yet. Thus we must begin to configure the Diagnostics framework.

Start by adding a class to your Web Application Project. You can do this at any point of the application as it will be compiled so it’s path is irrelevant. The name is also irrelevant, it is its inheritance hierarchy that is important.

Add a class to the application

Add a class to the application

This class you must make inherit from RoleEntryPoint:

RoleEntryPoint derivation

RoleEntryPoint derivation

As you can see, the RoleEntryPoint class is underlined by Visual Studio, this means in this case that the class isn’t found, because we are missing a reference and a using directive. You can add a reference to Microsoft.WindowsAzure.ServiceRuntime and add a using directive to solve this:

Add reference to Microsoft.WindowsAzure.ServiceRuntime

Add reference to Microsoft.WindowsAzure.ServiceRuntime

Add a using directive (Here shown using Intellisense)

Add a using directive (Here shown using Intellisense)

Now we can begin to add our code to configure the Windows Azure Diagnostics framework. This is done using the code from my previous blog. Firstly however, we must override the OnStart method in order to implement the code in the right place:

Override OnStart()

Override OnStart()

Now you can put the following code as the OnStart method:

        public override bool OnStart()
        {
            // put in here all the diagnostics setup that we want
            // see http://blog.bareweb.eu/2011/01/implementing-azure-diagnostics-with-sdk-v1-3/

            string wadConnectionString = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(wadConnectionString));

            RoleInstanceDiagnosticManager roleInstanceDiagnosticManager = storageAccount.CreateRoleInstanceDiagnosticManager(RoleEnvironment.DeploymentId, RoleEnvironment.CurrentRoleInstance.Role.Name, RoleEnvironment.CurrentRoleInstance.Id);
            DiagnosticMonitorConfiguration config = DiagnosticMonitor.GetDefaultInitialConfiguration();

            config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1D);

            config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Undefined;

            roleInstanceDiagnosticManager.SetCurrentConfiguration(config);  

            // note that a trace statement here will not append to the Windows Azure Diagnostics as there is no
            // trace listener set up for RoleEntryPoint.cs

            // make sure you have added the trace listener to web.config as per the blog http://blog.bareweb.eu/2011/01/migrating-to-azure-diagnostics-with-v1-3

            return base.OnStart();
        }

This will again cause a couple of reference errors, so you should add a reference to Microsoft.WindowsAzure.Diagnostics and Microsoft.WindowsAzure.StorageClient.

Your references will look like this once complete:

Complete References

Complete References

If you are using System.Diagnostics.Trace, you must also add a piece of configuration into your Web.Config in order to append trace messages to the new Diagnostics framework. If you are using Windows Event Logs, you do not have to do this, but you must adapt the above “OnStart” method to setup the transfer of Event Log details. Information on how to achieve transfer of Event Logs in Windows Azure can be found here.

The Web Config entry is:

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

Once you have completed this, you can then run your application, and any trace messages will be logged to the WADLogsTable in Development Storage:

Results in WADLogsTable

Results in WADLogsTable

You can find the source code for this here: Migrate to Windows Azure Diagnostics

About these ads