Quantcast
Channel: Wouter's blog » logging
Viewing all articles
Browse latest Browse all 2

Azure diagnostics. How to troubleshoot your code.

$
0
0

Introduction

Visual Studio 2013 includes a lot of great tools to make Azure development painless. But sometimes you need to diagnose, debug or troubleshoot programs installed in the actual Azure envirnoment. Here is a quick tutorial on how to enable and inspect debugging information in Azure.

Follow these steps to configure logging in an Azure application.

Step 1. Define listener

To enable logging you need to add an Azure DiagnosticMonitorTraceListener. You can simply configure this in the app.config or web.config of your application.

The sample below is from the app.config of a WorkRole.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.diagnostics>
    <trace>
      <listeners>
        <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
          <filter type="" />
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
  <appSettings>
...

Step 2. Get authorisation key

The logging (trace) informationin Azure is stored in an Azure Storage account. With some project types a storage account is automatically created when you publish from Visual Studio. If not you can always create a new one. Here is a screenshot of mine.
Azurestorageaccount

Click on your storage account. On the main dashboard page there will be a button at the bottom called: ‘Manage Access Keys’.
AzureManageKeysbutton

Click it.
You now get a pop-up where you can create/gererate access keys. Generate aone and temporarily copy the “primary access key” to notepad. We will need it later on.
AzureManageKeys

Step 3. Configure connectionstring

In the configuration file of your application configure a “diagnostic connectionstring”. Make sure your enter your Authorisation key correctly!

The sample below is taken from the file: ‘ServiceConfiguration.Cloud.cscfg’ of an Azure WorkerRole I created.

  <Role name="SiteProvisioning.WorkerRole">
    <Instances count="1" />
    <ConfigurationSettings>
      <Setting name="DiagnosticsConnectionString" value="DefaultEndpointsProtocol=https;AccountName=provisioningworkerrole;AccountKey=YourKey" />
...
    </ConfigurationSettings>

Replace ‘YourKey’ with the key you generated in step 2!

Step 4. Activate in code and usage.

Next step is to start the diagnostics in your application. Make sure that in the code below you rename “DiagnosticsConnectionString” to the name you defined in step 3.

public override bool OnStart()
{
   ServicePointManager.DefaultConnectionLimit = 12;
   var diagnosticMonitorConfiguration = DiagnosticMonitor.GetDefaultInitialConfiguration();
   diagnosticMonitorConfiguration.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
   diagnosticMonitorConfiguration.Logs.ScheduledTransferLogLevelFilter = Microsoft.WindowsAzure.Diagnostics.LogLevel.Verbose;
   DiagnosticMonitor.Start("DiagnosticsConnectionString", diagnosticMonitorConfiguration);

   return base.OnStart();
}

Use the .Net Trace infrastructure to log information to your log.

        private void ProcessItems()
        {
            try
            {
                Trace.TraceInformation("Working...");
                //TODO: do stuff
            }
            catch (Exception ex)
            {
                Trace.TraceError("Exception while processing: " + ex.Message, ex );
            }

            Trace.Flush();
        }

Inspecting the logs.

The trace information is stored in the table ‘WadLogsTable’ in your Azure Storage account. You can use Visual Studio to connect to this Storage Account. (create a connection)
I used a 3rd party tool to inspect the logs in Azure. Azure Management Studio. It’s not free but you can download a 30 day trial.

The tool is easy. You download a connection file from your Azure site. Import the file into the application and you can start using it.

You find your table containing the Trace information in:
YourSubscription — Storage Accounts — YourStorageName — Tables — WADLogsTable

azuremanagement

With compose button you can define a query. Handy if you want to limit the results to a certain timeframe.

That’s it. Happy debugging.



Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images