Application Insights & Azure Table Storage CreateIfNotExists Errors

When working with Azure Table Storage and Application Insights you might have noticed a lot of dependency errors, logging a 409 Conflict event everytime the CreateIfNotExists() method is called to make sure a table is available.

This is because the Azure.Data.Tables SDK will simply try to create a new table — and will hide the error if it already exists. While you don’t notice this in the code, by default Application Insights will catch this and clutter your logs with errors you probably don’t want.

To avoid this you can either configure the type of events Application Insights catches, or you can manually check if a table exists before trying to create it. Since this seamed a bit easier I’ve opted for option #2:

var tables = await tableServiceClient.QueryAsync(x => x.Name == table).ToListAsync().ConfigureAwait(false);
if (!tables.Any())
{
await tableServiceClient.CreateTableAsync(table).ConfigureAwait(false);
}

Note: I’m using the ToListAsync() method from the System.Linq.Async NuGet package to get all results from my queries without having to handle paging myself.

To make this even easier to use I published a NuGet package with this and more helpul extensions to the Azure.Data.Tables SKD. With that you can simply call 

// Create a table if it does not exists without throwing a hidden Exception that Application Insights will track
await tableServiceClient.CreateTableIfNotExistsSafeAsync(tableName);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.