protected override async Task WriteAsyncTask(IList <LogEventInfo> logEvents, CancellationToken token)
        {
            try
            {
                if (m_upload == null)
                {
                    lock (m_lock){
                        if (m_upload == null)
                        {
                            m_upload = new ConnatixKinesisUpload(AwsKey, AwsSecret, AwsRegion);
                        }
                    }
                }

                List <string> messages = new List <string>();
                foreach (var logEvent in logEvents)
                {
                    messages.Add(Layout.Render(logEvent));
                }

                await m_upload.WriteAsync(messages, Stream);
            }
            catch (Exception)
            {
            }
        }
Beispiel #2
0
        private void WriteToAmazon(object sender, ElapsedEventArgs args)
        {
            if (m_logMessages.Count == 0 || m_logMessages.Count < BatchSize && m_lastProcessed.AddSeconds(BatchInterval) > DateTime.Now)
            {
                return;
            }

            m_timer.Stop();

            try
            {
                if (m_upload == null)
                {
                    m_upload = new ConnatixKinesisUpload(AwsKey, AwsSecret, AwsRegion);
                }

                if (m_logMessages.Count > 0)
                {
                    List <string> messagesToWrite = new List <string>();
                    for (int i = 0; i < BatchSize; i++)
                    {
                        if (m_logMessages.TryDequeue(out var message))
                        {
                            messagesToWrite.Add(message);
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (messagesToWrite.Count > 0)
                    {
                        PutRecordsResponse response = m_upload.Write(messagesToWrite, Stream);
                        if (response.FailedRecordCount > 0)
                        {
                            for (int i = 0; i < response.Records.Count; i++)
                            {
                                var record = response.Records[i];
                                if (!string.IsNullOrEmpty(record.ErrorCode))
                                {
                                    m_logMessages.Enqueue(messagesToWrite[i]);
                                }
                            }
                        }
                    }
                }
            }
            catch {
            }
            finally
            {
                m_lastProcessed = DateTime.Now;
                m_timer.Start();
            }
        }