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
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
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()
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
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
. 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()
public override bool OnStart()
Trace.WriteLine("Testing Tracing through custom listener");
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. 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