public async Task QueueBackgroundDiagnosticsEventsTablePurge_PurgesTables() { IEnvironment testEnvironment = new TestEnvironment(); testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "0"); DiagnosticEventTableStorageRepository repository = new DiagnosticEventTableStorageRepository(_configuration, _hostIdProvider, testEnvironment, _logger); // delete any existing non-current diagnostics events tables string tablePrefix = DiagnosticEventTableStorageRepository.TableNamePrefix; var currentTable = repository.GetDiagnosticEventsTable(); var tables = await TableStorageHelpers.ListOldTablesAsync(currentTable, repository.TableClient, tablePrefix); foreach (var table in tables) { await table.DeleteIfExistsAsync(); } // create 3 old tables for (int i = 0; i < 3; i++) { var table = repository.TableClient.GetTableReference($"{tablePrefix}Test{i}"); await TableStorageHelpers.CreateIfNotExistsAsync(table, 2); } // verify tables were created tables = await TableStorageHelpers.ListOldTablesAsync(currentTable, repository.TableClient, tablePrefix); Assert.Equal(3, tables.Count()); // queue the background purge TableStorageHelpers.QueueBackgroundTablePurge(currentTable, repository.TableClient, tablePrefix, NullLogger.Instance, 0); // wait for the purge to complete await TestHelpers.Await(async() => { tables = await TableStorageHelpers.ListOldTablesAsync(currentTable, repository.TableClient, tablePrefix); return(tables.Count() == 0); }, timeout : 5000); }
internal virtual async Task FlushLogs(CloudTable table = null) { if (_environment.IsPlaceholderModeEnabled()) { return; } table = table ?? GetDiagnosticEventsTable(); try { bool tableCreated = await TableStorageHelpers.CreateIfNotExistsAsync(table, _tableCreationRetries); if (tableCreated) { TableStorageHelpers.QueueBackgroundTablePurge(table, TableClient, TableNamePrefix, _logger); } } catch (Exception ex) { _logger.LogError(ex, $"Unable to create table '{table.Name}' after {_tableCreationRetries} retries. Aborting write operation {ex}"); // Clearing the memory cache to avoid memory build up. _events.Clear(); return; } // Assigning a new empty directory to reset the event count in the new duration window. // All existing events are logged to other logging pipelines already. ConcurrentDictionary <string, DiagnosticEvent> tempDictionary = _events; _events = new ConcurrentDictionary <string, DiagnosticEvent>(); if (tempDictionary.Count > 0) { await ExecuteBatchAsync(tempDictionary, table); } }