Azure:以编程方式查询WADLogsTable以获取跟踪数据

Dav*_*vid 6 c# azure azure-storage azure-table-storage

我正在尝试使用以下代码从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();
Run Code Online (Sandbox Code Playgroud)

但是,当我知道最后一小时表中有数据时,我发现没有找到结果(我将输出与Cerebrata的Azure诊断管理器进行比较).

我有两个问题:

  1. 这是查询WADLogsTable的正确方法吗?为什么我没有看到任何结果?
  2. 作为泛型参数传递的正确类型是什么?TableServiceEntity是一个只定义三列的基类.我想知道是否有一种特定代表WADLogsTable实体的类型.我只是创建一个属性与列名相同的类型吗?

ast*_*kov 11

没有开箱即用的类型(类)代表WADLogs实体.使用基类,您将只获得PartionKey,RowKey和Timestamp属性.你必须自己定义它.这是我使用的示例:

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)));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

此外,当我在使用WADLogs表时,我设法使用此代码显示结果(过去24小时):

  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);
Run Code Online (Sandbox Code Playgroud)

我注意到在滴答计数之前分区键中有一个"0"前缀.

  • 谢谢,我现在有这个工作.它以前不起作用的原因是,由于某些极端的原因,你不能查询`Timestamp`列.最重要的是,安东. (2认同)