Azure: WADLogsTable programmatic request for trace data

I am trying to use the following code to get all the 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 found that no results were found when I know that the table contains data for the last hour (I compare the output with the Cerebrata Azure Diagnostic Manager).

I have two questions:

  • Is this the correct WADLogsTable request? Why can't I see any results?
  • What is the correct type to be passed as a generic parameter? TableServiceEntity is a base class that defines only three columns. I would like to know if there is a type representing WADLogsTable. Am I just creating a type with the properties the same as the column names?
+6
source share
2 answers

There is no type (class) that will be a WADLogs object. Using the base class, you will only get the PartionKey, RowKey, and Timestamp properties. You must determine this yourself. Here's the sample I'm using:

 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 was able to show the results (in the last 24 hours) using 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 noticed that before the key there is a prefix "0" in the section key.

+11
source

For users of the latest (2014) Azure Storage client:

http://blogs.msdn.com/b/tilovell/archive/2014/02/11/how-to-view-azure-diagnostics-traces-from-wadlogstable-in-your-local-console-app.aspx

tl; dr you can use timestamp to filter.

 ... var query = table.CreateQuery<GenericTableEntity>() .Where(e => e.Timestamp > DateTime.UtcNow.AddMinutes(-120)); 

Extending the object in a related example, you can open the Message and Date variables:

 public class LogEntity : GenericTableEntity { // Since Timestamp is a DateTimeOffset public DateTime LogDate { get { return Timestamp.UtcDateTime; } } public string Message { get { return Properties["Message"].StringValue; } } } 
-1
source

Source: https://habr.com/ru/post/910776/


All Articles