/// <summary>
        ///     Emit a batch of log events, running asynchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>
        ///     Override either <see cref="PeriodicBatchingSink.EmitBatch" /> or <see cref="PeriodicBatchingSink.EmitBatchAsync" />, not both.
        /// </remarks>
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            var allEvents = new StringWriter();

            foreach (var logEvent in events)
            {
                _jsonFormatter.Format(logEvent, allEvents);
            }

            var request  = new EventCollectorRequest(_splunkHost, allEvents.ToString(), _uriPath);
            var response = await _httpClient.SendAsync(request).ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            {
                //Application Errors sent via HTTP Event Collector
                if (HttpEventCollectorApplicationErrors.Any(x => x == response.StatusCode))
                {
                    // By not throwing an exception here the PeriodicBatchingSink will assume the batch succeeded and not send it again.
                    SelfLog.WriteLine(
                        "A status code of {0} was received when attempting to send to {1}.  The event has been discarded and will not be placed back in the queue.",
                        response.StatusCode.ToString(), _splunkHost);
                }
                else
                {
                    // EnsureSuccessStatusCode will throw an exception and the PeriodicBatchingSink will catch/log the exception and retry the batch.
                    response.EnsureSuccessStatusCode();
                }
            }
        }
Ejemplo n.º 2
0
        private async Task Send(IEnumerable <LogEvent> events)
        {
            string allEvents = string.Empty;

            foreach (var logEvent in events)
            {
                var sw = new StringWriter();
                _jsonFormatter.Format(logEvent, sw);

                var serialisedEvent = sw.ToString();

                var splunkEvent = new SplunkEvent(serialisedEvent, _source, _sourceType, _host, _index, logEvent.Timestamp.ToEpoch());

                allEvents = $"{allEvents}{splunkEvent.Payload}";
            }
            var request = new EventCollectorRequest(_splunkHost, allEvents);

            var response = await _httpClient.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                //Do Nothing?
            }
            else
            {
                //Application Errors sent via HTTP Event Collector
                if (HttpEventCollectorApplicationErrors.Any(x => x == response.StatusCode))
                {
                    SelfLog.WriteLine(
                        "A status code of {0} was received when attempting to send to {1}.  The event has been discarded and will not be placed back in the queue.",
                        response.StatusCode.ToString(), _splunkHost);
                }
                else
                {
                    //Put the item back in the queue & retry on next go
                    SelfLog.WriteLine(
                        "A status code of {0} was received when attempting to send to {1}.  The event has been placed back in the queue",
                        response.StatusCode.ToString(), _splunkHost);

                    foreach (var logEvent in events)
                    {
                        _queue.Enqueue(logEvent);
                    }
                }
            }
        }
        private async Task Send(IEnumerable <LogEvent> events)
        {
            var allEvents = new StringWriter();

            foreach (var logEvent in events)
            {
                _jsonFormatter.Format(logEvent, allEvents);
            }

            var request  = new EventCollectorRequest(_splunkHost, allEvents.ToString(), _uriPath);
            var response = await _httpClient.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                //Do Nothing?
            }
            else
            {
                //Application Errors sent via HTTP Event Collector
                if (HttpEventCollectorApplicationErrors.Any(x => x == response.StatusCode))
                {
                    SelfLog.WriteLine(
                        "A status code of {0} was received when attempting to send to {1}.  The event has been discarded and will not be placed back in the queue.",
                        response.StatusCode.ToString(), _splunkHost);
                }
                else
                {
                    //Put the item back in the queue & retry on next go
                    SelfLog.WriteLine(
                        "A status code of {0} was received when attempting to send to {1}.  The event has been placed back in the queue",
                        response.StatusCode.ToString(), _splunkHost);

                    foreach (var logEvent in events)
                    {
                        _queue.Enqueue(logEvent);
                    }
                }
            }
        }