Пример #1
0
        /// <summary>
        /// Patrolling thread. keeps tab on the PutLogEvent request and the
        /// Concurrent Queue
        /// </summary>
        private async Task Monitor(CancellationToken token)
        {
            try
            {
                token.ThrowIfCancellationRequested();

                if (_currentStreamName == null)
                {
                    _currentStreamName = await CreateLogStreamAsync(token).ConfigureAwait(false);
                }

                while (!token.IsCancellationRequested)
                {
                    if (_pendingMessageQueue.Count > 0)
                    {
                        var pendingMessages = Interlocked.Exchange(ref _pendingMessageQueue, new Queue <InputLogEvent>(200000));

                        var batches = LogEventBatch.CreateBatches(pendingMessages, _currentStreamName, _config);

                        foreach (var batch in batches)
                        {
                            await SendBatchAsync(batch, token).ConfigureAwait(false);
                        }
                    }
                    else
                    {
                        // If the logger is being terminated and all the messages have been sent, then exit out of loop.
                        // If there are messages left, then keep aggressively polling the message queue until it gets empty, so the process can die, but maximum it has 5 sec.
                        if (_isTerminated)
                        {
                            if (_pendingMessageQueue.Count == 0 || _terminationStopWatch.ElapsedMilliseconds > 5000)
                            {
                                break;
                            }
                            else
                            {
                                continue;
                            }
                        }

                        await Task.Delay(TimeSpan.FromMilliseconds(_config.MonitorSleepTime.TotalMilliseconds)).ConfigureAwait(false);
                    }
                }
            }
            catch (OperationCanceledException oc)
            {
                LogLibraryError(oc, _config.LibraryLogFileName);
                throw;
            }
            catch (AmazonServiceException amazonEx)
            {
                LogLibraryError(amazonEx, _config.LibraryLogFileName);

                if (!TransientErrorCodes.Any(x => amazonEx.ErrorCode.Equals(x)))
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                LogLibraryError(ex, _config.LibraryLogFileName);
            }
        }