Truncating Azure Logs

The following demonstates how to delete from WADLogsTable from before a given date.
It may not be the most efficient code but it does work.

This has been tested against Azure SDK 2.0

///

/// TruncateDiagnostics(storageAccount, DateTime.Now.AddHours(-1));
///

///
///
public void TruncateDiagnostics(CloudStorageAccount storageAccount, DateTime keepThreshold)
{
try
{

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

CloudTable cloudTable = tableClient.GetTableReference(“WADLogsTable”);

TableQuery query = new TableQuery();
query.FilterString = string.Format(“Timestamp lt datetime'{0:yyyy-MM-ddTHH:mm:ss}'”, keepThreshold);
var items = cloudTable.ExecuteQuery(query).ToList();

Dictionary batches = new Dictionary();
foreach (var entity in items)
{
TableOperation tableOperation = TableOperation.Delete(entity);

if (!batches.ContainsKey(entity.PartitionKey))
{
batches.Add(entity.PartitionKey, new TableBatchOperation());
}

batches[entity.PartitionKey].Add(tableOperation);
}

foreach (var batch in batches.Values)
{
cloudTable.ExecuteBatch(batch);
}

}
catch (Exception ex)
{
Trace.TraceError(string.Format(“Truncate WADLogsTable exception {0}”, ex), “Error”);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s