in src/WebJobs.Script.WebHost/Diagnostics/DiagnosticEventTableStorageRepository.cs [208:265]
internal virtual async Task FlushLogs(TableClient table = null)
{
// TableClient is initialized lazily and it will stop the timer that schedules flush logs whenever it fails to initialize.
// We need to check if the TableClient is null before proceeding. This helps when the first time the property is accessed is as part of the FlushLogs method.
// We should not have any events stored pending to be written since WriteDiagnosticEvent will check for an initialized TableClient.
if (_environment.IsPlaceholderModeEnabled() || TableClient is null || !IsEnabled())
{
return;
}
if (IsPrimaryHost() && !_purged)
{
await PurgePreviousEventVersions();
}
try
{
table = table ?? GetDiagnosticEventsTable();
if (table == null)
{
Logger.UnableToGetTableReference(_logger);
DisableService();
return;
}
bool tableCreated = await TableStorageHelpers.CreateIfNotExistsAsync(table, TableClient, TableCreationMaxRetryCount);
if (tableCreated)
{
Logger.QueueingBackgroundTablePurge(_logger);
TableStorageHelpers.QueueBackgroundTablePurge(table, TableClient, TableNamePrefix, _logger);
}
}
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.Forbidden)
{
// If we reach this point, we already checked for permissions on TableClient initialization. It is possible that the permissions changed after the initialization or any storage firewall/network configuration changed.
// We will log the error and disable the service.
Logger.UnableToGetTableReferenceOrCreateTable(_logger, ex);
DisableService();
Logger.ServiceDisabledUnauthorizedClient(_logger, ex);
}
catch (Exception ex)
{
Logger.UnableToGetTableReferenceOrCreateTable(_logger, 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.IsEmpty)
{
await ExecuteBatchAsync(tempDictionary, table);
}
}