Mah
Mah

Reputation: 77

why I can not add multiple rows to azure table?

I try to add multiple rows to azure storage: I make a report and add it to the azure table, but it add only the last row, I think it replaces it.

public void StoreReport()
{
    try
    {
        var timeStamp = DateTime.Now;
        var logsWithError = new List<ReportItem>();

        foreach (var testCategory in Logs)
        {
            foreach (var testItem in Logs[testCategory.Key])
            {
                if (testItem.Value.Any(x => x.LogType == LogType.Error))
                {
                    var reports = testItem.Value.Select(x => new ReportItem()
                    {
                        PartitionKey = timeStamp.ToString("yyyy-MM-dd HH:mm:ss"),
                        RowKey = testCategory.Key,
                        TestItem = testItem.Key,
                        TestRunStatus = x.LogType.ToString(),
                        TestRunStatusDetail = x.Message,
                        LogTimeStamp = x.TimeStamp
                    });
                    logsWithError.AddRange(reports);
                }
            }
        }

        azureService.AddToAzureTable(logsWithError);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

public async Task AddToAzureTable(List<ReportItem> items)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageAccountConnectionString);
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    CloudTable table = tableClient.GetTableReference(tableName);

    foreach (var item in items)
    {
        await table.ExecuteAsync(TableOperation.InsertOrReplace(item));
    }
}

Upvotes: 0

Views: 312

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136394

Since AddToAzureTable is an async method, you will have to await the execution of it. Please try by changing following line of code:

azureService.AddToAzureTable(logsWithError);

to

await azureService.AddToAzureTable(logsWithError);

You will need to make StoreReport and other upstream methods async as well.

Upvotes: 1

Related Questions