Windows Azure logging

Windows Azure has built in logging support, which can be used in any Azure application. Azure use System.Diagnostic namespace and have a number of static methods on Trace class

Trace.TraceError(...); Trace.TraceWarning(...); Trace.TraceInformation(...); Trace.Write(...);

The advantage of using this methods is when you running application in emulator locally you have all your messages appear in console windows, but when you deploy your application into the cloud, Windows Azure will log everything into WADLogsTable, which will be created automatically for you. As a developer you have to do nothing.

WADLogsTable structure

The structure of WADLogsTable is pretty standard for log tables, the most important fields for you will be PartitionKey, Level and Message Message is the log message which you output using Trace class Level is logging level, such as error or info (that is numeric field, so for example Error is 2) EventTickCount datetime in ticks when event occurred (it will be different from the Timestamp field, because timestamp is when record was add to log table and usually happen later then event was recorded) PartitionKey - that is interesting field for you, especially if you have a lot of records and want to sort data in the table by date. Below is a little more about this field

How to sort data in WADLogsTable using Partition key

If you will look at PartitionKey, you will find then they look very similar to EventTickCount, except PartitionKey has 0 at the beginning and zeroes at the end, for example


So we can easy convert this number to datetime and convert any datetime back to partition key But what it gives to us? We have Timestamp field, can we use Timestamp? The reason to use Partition key instead of timestamp is PartitionKey indexed but Timestamp is not, as a result if you will try to query big table using Timestamp it will take a lot of time, but with PartitionKey it will be much faster.

How to convert datetime to partitionkey format?

The easiest way is to use LINQPad

Why my logs table is empty?

By default Windows Azure log into WADLogsTable only errors, so if you try to TraceInformation or TraceWarning, you will see nothing (and I spent an hour to figure out why). But this can be changed in the settings of the instance of your cloud application. Just right click on your role and go to properties and you will have a number of options in Diagnostics section. Just be aware then if you choose All Information, you will have a lot of messages posted by Windows Azure framework, so the logs table can became very big soon. change logging level in azure table

Log4Net appender for Windows Azure

But what will be the best way to log your messages in WADLogsTable? You don't want to use Trace.TraceError all the time. I can suggest to create appender for your favorite logging framework, for example below is a short and simple appender code for log4net


Social media


Latest Tweets