.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> Azure >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Windows Azure – Logging : Azure Part 46

Posted By:Jean Paul       Posted Date: June 08, 2012    Points: 200    Category: Azure    URL: http://www.dotnetspark.com  

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

 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 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 can examine the logging of information into Storage Account Table.  As the table provides us the flexibility of defining columns I think it would be a better choice than queue.
Following are the steps involved:
  • Create Entity
  • Create Custom Trace Listener
  • Integrate the Listener
  • View the Table
Step 1: Create 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

    public DateTime EventTime

Step 2: Create Custom Trace Listener
You will be aware that the Trace class is of much flexibility.  It will be having default listeners and it allows adding custom listeners too.  Here we are creating a custom listener class by inheriting from TraceListener.  On 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)

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

        // 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);
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.

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

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.

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

So this confirms our custom listener integration.
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.

 Subscribe to Articles


Further Readings:

Author: Sreeraj Nair Mungath         Company URL: http://www.dotnetspark.com
Posted Date: July 12, 2012

good..but i need somemore

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend