How to view Azure Diagnostics Traces from WADLogsTable in your local console app
If you use Azure Diagnostics in your cloud service, and you use the DiagnosticTraceListener then all of your traces are getting output to a table somewhere called WADLogsTable.
I think it’s definitely a nuisance having to use the Visual Studio’s table viewer or some other program to see those logs. So anyway, to save you from purchasing or downloading some viewer app, here is a very bare bones console app you can run to see what you’ve just output to that table. I may get around to improving this later, I may not.
You may want to hardcode your own connection string in, tweak the time to start reading logs from or even feel excited ot make other improvements like paging and optimized partition queries based on reverse engineering Windows Azure Diagnostics partition key… feel free.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
namespace wadlogs
{
class Program
{
static void Main(string[] args)
{
DateTime utcStartTime = DateTime.UtcNow;
Console.WriteLine("Enter your storage account connection string, or press enter for 'UseDevelopmentStorage=true;':");
var connStr = Console.ReadLine();
connStr = connStr.Trim(new[] { ' ', '\"', '\'', });
if (string.IsNullOrEmpty(connStr))
{
connStr = "UseDevelopmentStorage=true;";
}
var storageAccount = CloudStorageAccount.Parse(connStr);
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference("WADLogsTable");
var query = table.CreateQuery<GenericTableEntity>()
.Where(e => e.Timestamp > utcStartTime.AddMinutes(-120));
var results = query.ToList();
results = results.OrderBy(x => x.Timestamp).ToList();
foreach (var row in results)
{
Console.WriteLine("{0}: {1}", row.Timestamp, row.Properties["Message"].StringValue);
}
}
}
public class GenericTableEntity : ITableEntity
{
public string ETag { get; set; }
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public DateTimeOffset Timestamp { get; set; }
public IDictionary<string, EntityProperty> Properties { get; set; }
public void ReadEntity(IDictionary<string, EntityProperty> properties, Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
this.Properties = properties;
}
public IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
return this.Properties;
}
}
}