0

I am trying to delete data from my WADLogsTable using the solution provided here

I have modified it a bit to call developmentstorage but the code throws a exception with The remote server returned an error: (400) Bad Request. saying "One of the request inputs is out of range." after executing this line of code if(items.Count() == 0)

following is my code

public static async void TruncateDiagnostics()
    {
        Uri test = new Uri("http://127.0.0.1:10002/Tables/");
        while (true)
        {
            try
            {
                // Retrieve storage account from connection-string
                CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
                Microsoft.WindowsAzure.Storage.Table.CloudTableClient tableClientObj = new Microsoft.WindowsAzure.Storage.Table.CloudTableClient(test); 
                CloudTable cloudTable = tableClientObj.GetTableReference("wadlogstable");

                // keep a hours worth of logs
                DateTime keepThreshold = DateTime.UtcNow.AddHours(-1);

                // do this until we run out of items
                while (true)
                {
                    TableQuery query = new TableQuery();
                    query.FilterString = string.Format("PartitionKey < '0{0}'", keepThreshold.Ticks);
                    var items = cloudTable.ExecuteQuery(query);

                    if (items.Count() == 0)
                        break;

                    Dictionary<string, TableBatchOperation> batches = new Dictionary<string, TableBatchOperation>();
                    foreach (var entity in items)
                    {
                        TableOperation tableOperation = TableOperation.Delete(entity);

                        // need a new batch?
                        if (!batches.ContainsKey(entity.PartitionKey))
                            batches.Add(entity.PartitionKey, new TableBatchOperation());

                        // can have only 100 per batch
                        if (batches[entity.PartitionKey].Count < 100)
                            batches[entity.PartitionKey].Add(tableOperation);
                    }

                    // execute!
                    foreach (var batch in batches.Values)
                        await cloudTable.ExecuteBatchAsync(batch);

                    Trace.TraceInformation("WADLogsTable truncated: " + query.FilterString);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Truncate WADLogsTable exception {0}", ex.Message);
            }

            // run this once per day
            await Task.Delay(TimeSpan.FromDays(1));
        }
    }

I am stuck with this issue for some time now. Any help will be appreciated. Thanks in advance.

Community
  • 1
  • 1
Karthik Ganesan
  • 4,142
  • 2
  • 26
  • 42

1 Answers1

0

try:

query.FilterString = string.Format("PartitionKey gt '0{0}'", keepThreshold.Ticks);
query.FilterString = string.Format("PartitionKey lt '0{0}'", keepThreshold.Ticks);

The query is not executed until your foreach loop