Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure: programmatically querying WADLogsTable for trace data

I'm trying to use the following code to get all trace data for the last hour from Azure:

                StorageCredentialsAccountAndKey storageCredentialsAccountAndKey = new StorageCredentialsAccountAndKey(accountName, key);
                CloudStorageAccount csa = new CloudStorageAccount(storageCredentialsAccountAndKey, true);
                TableServiceContext tableServiceContext = new TableServiceContext(csa.TableEndpoint.ToString(), csa.Credentials);
                var results = tableServiceContext.CreateQuery<TableServiceEntity>("WADLogsTable").Where(
                    x => x.Timestamp > DateTime.UtcNow.AddHours(-1)).ToList();

However, I'm finding that no results are found when I know that there is data in the table for the last hour (I'm comparing the output to Cerebrata's Azure Diagnostics Manager).

I have two questions:

  1. Is this the right way to query WADLogsTable? Why am I not seeing any results?
  2. What is the correct type to pass in as the generic parameter? TableServiceEntity is a base class that only defines three columns. I'd like to know if there is a type that represents a WADLogsTable entity specifically. Do I just create a type with properties the same as the column names?
like image 544
David Avatar asked Mar 15 '12 09:03

David


1 Answers

There is no out of the box type (class) that would represent WADLogs entity. Using the base class you will only get the PartionKey, RowKey and Timestamp properties. You have to define it by yourself. Here a sample that I use:

public class WadLogEntity
       : Microsoft.WindowsAzure.StorageClient.TableServiceEntity
    {
        public WadLogEntity()
        {
            PartitionKey = "a";
            RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());
        }

        public string Role { get; set; }
        public string RoleInstance { get; set; }
        public int Level { get; set; }
        public string Message { get; set; }
        public int Pid { get; set; }
        public int Tid { get; set; }
        public int EventId { get; set; }
        public DateTime EventDateTime
        {
            get
            {
                return new DateTime(long.Parse(this.PartitionKey.Substring(1)));
            }
        }
    }

Also, when I was struggling with the WADLogs table, I managed to get it showing the results (for the last 24 hours) with this code:

  var dtThen = DateTime.UtcNow.AddHours(-24);
                var dtNow = DateTime.UtcNow;

                var logs = this._wadLogs.WadLogs.Where(
                    wl => 
                        wl.Level == 2 
                        && String.Compare(wl.PartitionKey,"0" + dtThen.Ticks.ToString()) >=0
                        && String.Compare(wl.PartitionKey, "0" + dtNow.Ticks.ToString()) < 0
                    ).Take(200);

I noted that there is a "0" prefix in the partition key before the ticks count.

like image 70
astaykov Avatar answered Sep 28 '22 07:09

astaykov