private List <String> SerializeEvents(IEnumerable <LogEvent> events)
        {
            List <string> chunks = new List <string>();

            int currentSize = 0;

            var chunkBuffer = new List <string>(events.Count());

            foreach (var logEvent in events)
            {
                var formattedLog = _formatter.formatMessage(logEvent);
                var logSize      = Encoding.UTF8.GetByteCount(formattedLog);
                if (logSize > _maxMessageSize)
                {
                    continue;  // The log is dropped because the backend would not accept it
                }
                if (currentSize + logSize > _maxSize)
                {
                    // Flush the chunkBuffer to the chunks and reset the chunkBuffer
                    chunks.Add(GenerateChunk(chunkBuffer, ",", "[", "]"));
                    chunkBuffer.Clear();
                    currentSize = 0;
                }
                chunkBuffer.Add(formattedLog);
                currentSize += logSize;
            }
            chunks.Add(GenerateChunk(chunkBuffer, ",", "[", "]"));

            return(chunks);
        }
Пример #2
0
        public async Task WriteAsync(IEnumerable <LogEvent> events)
        {
            var payloadBuilder = new StringBuilder();

            foreach (var logEvent in events)
            {
                payloadBuilder.Append(_apiKey + WhiteSpace);
                payloadBuilder.Append(_formatter.formatMessage(logEvent));
                payloadBuilder.Append(MessageDelimiter);
            }

            var payload = payloadBuilder.ToString();

            for (var retry = 0; retry < MaxRetries; retry++)
            {
                var backoff = (int)Math.Min(Math.Pow(retry, 2), MaxBackoff);
                if (retry > 0)
                {
                    await Task.Delay(backoff * 1000);
                }

                if (IsConnectionClosed())
                {
                    try
                    {
                        await ConnectAsync();
                    }
                    catch (Exception e)
                    {
                        SelfLog.WriteLine("Could not connect to Datadog: {0}", e);
                        continue;
                    }
                }

                try
                {
                    var data = UTF8.GetBytes(payload);
                    _stream.Write(data, 0, data.Length);
                    return;
                }
                catch (Exception e)
                {
                    CloseConnection();
                    SelfLog.WriteLine("Could not send data to Datadog: {0}", e);
                }
            }

            SelfLog.WriteLine("Could not send payload to Datadog: {0}", payload);
        }
        public async Task WriteAsync(IEnumerable <LogEvent> events)
        {
            var payloadBuilder = new StringBuilder();

            foreach (var logEvent in events)
            {
                payloadBuilder.Append(_apiKey + WhiteSpace);
                payloadBuilder.Append(_formatter.formatMessage(logEvent));
                payloadBuilder.Append(MessageDelimiter);
            }
            var payload = payloadBuilder.ToString();

            if (_retry > 0)
            {
                var backoff = (int)Math.Min(Math.Pow(2, _retry), MaxBackoff);
                await Task.Delay(backoff * 1000);
            }

            _retry++;

            if (IsConnectionClosed())
            {
                await ConnectAsync();
            }

            try {
                var data = UTF8.GetBytes(payload);
                _stream.Write(data, 0, data.Length);
                _retry = 0;
            }
            catch (Exception) {
                CloseConnection();
                throw;
            }

            SelfLog.WriteLine("Could not send payload to Datadog after {_retry} retries. Payload: {0}", payload);
        }