Beispiel #1
0
 public EnrichingReader(
     ILogEventReader inner,
     IReadOnlyCollection <ILogEventEnricher> enrichers)
 {
     _inner     = inner ?? throw new ArgumentNullException(nameof(inner));
     _enrichers = enrichers ?? throw new ArgumentNullException(nameof(enrichers));
 }
Beispiel #2
0
        public static async Task <int> ShipEvents(
            SeqConnection connection,
            ILogEventReader reader,
            List <ILogEventEnricher> enrichers,
            InvalidDataHandling invalidDataHandling,
            SendFailureHandling sendFailureHandling,
            Func <LogEvent, bool> filter = null)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (enrichers == null)
            {
                throw new ArgumentNullException(nameof(enrichers));
            }

            var batch = await ReadBatchAsync(reader, filter, BatchSize, invalidDataHandling);

            while (true)
            {
                var sendSucceeded = false;
                try
                {
                    sendSucceeded = await SendBatchAsync(
                        connection,
                        batch.LogEvents,
                        enrichers,
                        sendFailureHandling != SendFailureHandling.Ignore);
                }
                catch (Exception ex)
                {
                    if (sendFailureHandling != SendFailureHandling.Ignore)
                    {
                        Log.Error(ex, "Failed to send an event batch");
                    }
                }

                if (!sendSucceeded && sendFailureHandling == SendFailureHandling.Fail)
                {
                    return(1);
                }

                if (batch.IsLast)
                {
                    break;
                }

                batch = await ReadBatchAsync(reader, filter, BatchSize, invalidDataHandling);
            }

            return(0);
        }
Beispiel #3
0
        static async Task <BatchResult> ReadBatchAsync(
            ILogEventReader reader,
            Func <LogEvent, bool> filter,
            int count,
            InvalidDataHandling invalidDataHandling)
        {
            var batch  = new List <LogEvent>();
            var isLast = false;

            do
            {
                try
                {
                    while (batch.Count < count)
                    {
                        var rr = await reader.TryReadAsync();

                        isLast = rr.IsAtEnd;
                        var evt = rr.LogEvent;
                        if (evt == null)
                        {
                            break;
                        }

                        if (filter == null || filter(evt))
                        {
                            batch.Add(evt);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ex is JsonReaderException || ex is InvalidDataException)
                    {
                        if (invalidDataHandling == InvalidDataHandling.Ignore)
                        {
                            continue;
                        }
                    }

                    throw;
                }

                return(new BatchResult(batch.ToArray(), isLast));
            } while (true);
        }
 public StaticMessageTemplateReader(ILogEventReader inner, string messageTemplate)
 {
     _inner           = inner ?? throw new ArgumentNullException(nameof(inner));
     _messageTemplate = new MessageTemplateParser().Parse(messageTemplate);
 }
Beispiel #5
0
        public static async Task <int> ShipEvents(
            SeqConnection connection,
            string apiKey,
            ILogEventReader reader,
            InvalidDataHandling invalidDataHandling,
            SendFailureHandling sendFailureHandling,
            int batchSize,
            Func <LogEvent, bool> filter = null)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var batch = await ReadBatchAsync(reader, filter, batchSize, invalidDataHandling);

            var retries = 0;

            while (true)
            {
                var sendSucceeded = false;
                try
                {
                    sendSucceeded = await SendBatchAsync(
                        connection,
                        apiKey,
                        batch.LogEvents,
                        sendFailureHandling != SendFailureHandling.Ignore);
                }
                catch (Exception ex)
                {
                    if (sendFailureHandling != SendFailureHandling.Ignore)
                    {
                        Log.Error(ex, "Failed to send an event batch");
                    }
                }

                if (!sendSucceeded)
                {
                    if (sendFailureHandling == SendFailureHandling.Fail)
                    {
                        return(1);
                    }

                    if (sendFailureHandling == SendFailureHandling.Retry)
                    {
                        var millisecondsDelay = (int)Math.Min(Math.Pow(2, retries) * 2000, 60000);
                        await Task.Delay(millisecondsDelay);

                        retries += 1;
                        continue;
                    }
                }

                retries = 0;

                if (batch.IsLast)
                {
                    break;
                }

                batch = await ReadBatchAsync(reader, filter, batchSize, invalidDataHandling);
            }

            return(0);
        }