Esempio n. 1
0
        private async Task RunUploader()
        {
            TimeSpan errorDelay = _serverErrorBackoffSettings.Delay;

            while (true)
            {
                IEnumerable <LogEntryExtra> entries;
                // Wait/loop until there are some log entries that need uploading.
                // TODO: See if log4net can be shutdown. If so, then this loop needs to terminate after
                // all log entries have been uploaded.
                while (true)
                {
                    DateTimeRange logEntriesLost;
                    lock (_lockObj)
                    {
                        logEntriesLost  = _logEntriesLost;
                        _logEntriesLost = null;
                    }
                    entries = await _logQ.PeekAsync(_maxUploadBatchSize - (logEntriesLost != null ? 1 : 0));

                    if (logEntriesLost != null)
                    {
                        var lostEntryExtra = new LogEntryExtra(-1, MakeLostEntry(logEntriesLost));
                        entries = (new[] { lostEntryExtra }).Concat(entries ?? Enumerable.Empty <LogEntryExtra>());
                    }
                    if (entries != null && entries.Count() > 0)
                    {
                        // There are log entries that need uploading, so do that now.
                        break;
                    }
                    await _uploadReadyEvent.WaitAsync();
                }
                // Upload entries to the Cloud Logging server
                try
                {
                    await _client.WriteLogEntriesAsync("", null, s_emptyLabels, entries.Select(x => x.Entry));

                    await _logQ.RemoveUntilAsync(entries.Last().Id);

                    _emptyEvent.Set();
                    errorDelay = _serverErrorBackoffSettings.Delay;
                }
                catch (Exception)
                {
                    // Always retry, regardless of error, as there's nothing much else to be done.
                    await _scheduler.Delay(errorDelay);

                    errorDelay = errorDelay.Scale(_serverErrorBackoffSettings.DelayMultiplier);
                    if (errorDelay > _serverErrorBackoffSettings.MaxDelay)
                    {
                        errorDelay = _serverErrorBackoffSettings.MaxDelay;
                    }
                }
            }
        }
 public LogEntryExtraSize(LogEntryExtra logEntryExtra, int size)
 {
     LogEntryExtra = logEntryExtra;
     Size          = size;
 }