Пример #1
0
        protected override async Task <int> Run()
        {
            var outputConfiguration = new LoggerConfiguration()
                                      .MinimumLevel.Is(LevelAlias.Minimum)
                                      .Enrich.With <RedundantEventTypeRemovalEnricher>()
                                      .Enrich.With <SurrogateLevelRemovalEnricher>()
                                      .WriteTo.Console(
                outputTemplate: _template,
                theme: _noColor ? ConsoleTheme.None : OutputFormatFeature.DefaultTheme);

            if (_filter != null)
            {
                outputConfiguration.Filter.ByIncludingOnly(_filter);
            }

            using (var logger = outputConfiguration.CreateLogger())
            {
                foreach (var input in _fileInputFeature.OpenInputs())
                {
                    using (input)
                    {
                        var reader = new JsonLogEventReader(input);

                        var isAtEnd = false;
                        do
                        {
                            try
                            {
                                var result = await reader.TryReadAsync();

                                isAtEnd = result.IsAtEnd;

                                if (result.LogEvent != null)
                                {
                                    logger.Write(result.LogEvent);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (!(ex is JsonReaderException) && !(ex is InvalidDataException) ||
                                    _invalidDataHandlingFeature.InvalidDataHandling != InvalidDataHandling.Ignore)
                                {
                                    throw;
                                }
                            }
                        } while (!isAtEnd);
                    }
                }
            }

            return(0);
        }
Пример #2
0
        protected override async Task <int> Run()
        {
            try
            {
                var enrichers = new List <ILogEventEnricher>();
                foreach (var(name, value) in _properties.Properties)
                {
                    enrichers.Add(new ScalarPropertyEnricher(name, value));
                }

                if (_level != null)
                {
                    enrichers.Add(new ScalarPropertyEnricher(SurrogateLevelProperty.PropertyName, _level));
                }

                Func <LogEvent, bool> filter = null;
                if (_filter != null)
                {
                    var expr = _filter.Replace("@Level", SurrogateLevelProperty.PropertyName);
                    var eval = FilterLanguage.CreateFilter(expr);
                    filter = evt => true.Equals(eval(evt));
                }

                var connection = _connectionFactory.Connect(_connection);
                var(_, apiKey) = _connectionFactory.GetConnectionDetails(_connection);
                var batchSize = _batchSize.Value;

                foreach (var input in _fileInputFeature.OpenInputs())
                {
                    using (input)
                    {
                        var reader = _json
                            ? (ILogEventReader) new JsonLogEventReader(input)
                            : new PlainTextLogEventReader(input, _pattern);

                        reader = new EnrichingReader(reader, enrichers);

                        if (_message != null)
                        {
                            reader = new StaticMessageTemplateReader(reader, _message);
                        }

                        var exit = await LogShipper.ShipEvents(
                            connection,
                            apiKey,
                            reader,
                            _invalidDataHandlingFeature.InvalidDataHandling,
                            _sendFailureHandlingFeature.SendFailureHandling,
                            batchSize,
                            filter);

                        if (exit != 0)
                        {
                            return(exit);
                        }
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Ingestion failed: {ErrorMessage}", ex.Message);
                return(1);
            }
        }