Search code examples
azure-storageazure-diagnosticswadslogtable

How to fetch latest below 1000 WADLogsTable entry?


I've coded for fetching latest diagnostic log from wadlogstable in c# But it is traversing all over records and then giving latest entry ex. there 5000 records in table but i want only latest or last 1000 record but it is giving all records then after order by query it is giving last 1000 record, so it is very time consuming, it is taking almost 7-8 minutes to fecth 4000-5000 records

 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ATCommon.DiagnosticConfig);
        CloudTableClient cloudTableClient = storageAccount.CreateCloudTableClient();
        TableServiceContext serviceContext = cloudTableClient.GetDataServiceContext();
        IQueryable<WadLogEntity> traceLogsTable = serviceContext.CreateQuery<WadLogEntity>("WADLogsTable");
        var selection = from row in traceLogsTable where row.PartitionKey.CompareTo("0" + DateTime.UtcNow.AddHours(hours).Ticks) >= 0 select row;
        //var selection = from row in traceLogsTable where row.PartitionKey.CompareTo("0" + DateTime.UtcNow.AddMinutes(-5.0).Ticks) >= 0 select row;
        CloudTableQuery<WadLogEntity> query = selection.AsTableServiceQuery<WadLogEntity>();
        IEnumerable<WadLogEntity> output = query.Execute();
return output.OrderByDescending(s => s.Timestamp).ToList();

Solution

  • I have more then 500 million of entries. and every second it is adding more..

    string apiLogTableName = "yourtableName";
    StorageTable apiLogsTable = new StorageTable(apiLogTableName);
    
     string filter1 = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, date);   //hear you can check with ticks
    
    
     string filter2 = TableQuery.GenerateFilterConditionForInt("yourColumn", QueryComparisons.Equal, yourValue);
    
                string filter3 = TableQuery.GenerateFilterCondition("YourColumn2", QueryComparisons.NotEqual, "YourValue2");
    
    
    
                TableQuery<ApiCallLogEntity> findapiLogsRecord = new TableQuery<ApiCallLogEntity>().Where(TableQuery.CombineFilters(
    
                    TableQuery.CombineFilters(
                                filter1,
                                TableOperators.And,
                                filter2),
                    TableOperators.And, filter3));
    
    //you can use
    //var records= apiLogsTable._table.ExecuteQuery(findapiLogsRecord)
    
    
    //bellow code will get one-one records // time consuming
                foreach (ApiCallLogEntity entity in apiLogsTable._table.ExecuteQuery(findapiLogsRecord))
                {
                    Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
                        entity.Action, entity.RequestBody);
    
    
                    tw.WriteLine("{0}, {1}\t{2}\t{3}\t{4}", entity.PartitionKey, entity.RowKey,
                        entity.Action, entity.RequestBody, entity.ResponseBody);
                }