Windows Azure- Logging

In the production deployment, sometimes we need to trace the inner workings of our application. In ASP.NET and WinForms application we can trace the information into a file. But in the world of Azure accessing the files are a  little bit difficult. Here we can use the following methods to trace information.

  • Writing Trace to Storage Account Table
  • Writing Trace to Storage Account Queue
  • Writing Trace to SQL Azure
  • Emailing Trace to Administrator

Logging Information

In this article we examine the logging of information into a Storage Account Table. As the table provides us the flexibility of defining columns I think it would be a better choice than queue.

The following are the steps involved:

  • Create Entity
  • Create Custom Trace Listener
  • Integrate the Listener
  • View the Table

Step 1: Create an Entity

For storing the logging information we can define an entity. As we are planning to store this in the Storage Table we need to derive the entity from TableServiceEntity.

public class LogEntry : TableServiceEntity
{
   
public string Message
    {
       
get;
       
set;
    }

    public DateTime EventTime
    {
       
get;
       
set;
    }
}


Step 2: Create a Custom Trace Listener

You will be aware that the Trace class has much flexibility. It will have the default listeners and it allows adding custom listeners too. Here we are creating a custom listener class by inheriting from TraceListener. For deriving from it we need to implement the abstract Write() and WriteLine() methods which are abstract.

public class MyListener : TraceListener
{
   
public override void Write(string message)
    {
WriteLine(message);
    }

    public override void WriteLine(string message)
    {
       
StorageCredentialsAccountAndKey accountAndKey = new StorageCredentialsAccountAndKey("YourAccount", "YourKey");
       
CloudStorageAccount account = new CloudStorageAccount(accountAndKey, true);
       
CloudTableClient client = account.CreateCloudTableClient();
        client.CreateTableIfNotExist(
"Log");

        // Create table context
        TableServiceContext tableContext = new TableServiceContext(account.TableEndpoint.ToString(), account.Credentials);

        // Create entity
        LogEntry log = new LogEntry();
        log.PartitionKey =
Guid.NewGuid().ToString();
        log.RowKey =
Guid.NewGuid().ToString();
        log.Message = message;
        log.EventTime =
DateTime.Now;

        // Add entity to table and save the changes
        tableContext.AddObject("Log", log);
        tableContext.SaveChanges();
    }
}

The WriteLine() method stores the message into Storage Account table.

In the above code the Account name and Key has to be placed in the appropriate positions. For creating storage account, you can refer this article.

Step 3: Integrate the Listener

Now we can integrate the listener to our application. Create a new web role project and add the following code into the WebRole OnStart() method.

public override bool OnStart()
{
   
Trace.Listeners.Add(new MyListener());

    Trace.WriteLine("Testing Tracing through custom listener");
   
Trace.WriteLine("Another message");

    return base.OnStart();
}

Now press F5 to execute the application

Step 4: View the Table

After executing the application, we can view the result in the Table. Open the Server Explorer window from Visual Studio and connect to the Azure account.

You can use the Windows Azure Storage item as shown below.

WinAzr1.gif

From the item right click and choose Add new Storage Account.

WinAzr2.gif

Use your account name and key for connecting.

After connecting expand the Tables item and you can see the table named Log as shown below.

WinAzr3.gif

Right click on the Log table and use the View Table option. You can see our trace information recorded there.

WinAzr4.gif

So this confirms our custom listener integration.  

In the real world scenario, you can invoke the Logging method from Exception Handlers or Global Exception Event.   You can use the IsEmulated property of Azure runtime to switch between local and online table storage.

Summary

In this article we have seen one of the methods of tracing information on Windows Azure. We can also integrate the custom listener using configuration file and extract the account name and key out of the application. The attached source code contains the application explained above.
 


Similar Articles