private async Task ParseRfc3164Log(LineReader reader, LogContext context, IList <IEnvelope <SyslogData> > output,
                                           int recordCount, CancellationToken stopToken)
        {
            var packet = new Rfc3164Packet();
            var parser = new Rfc3164Parser(new Rfc3164ParserOptions
            {
                RequirePri  = false,
                DefaultYear = DateTime.Now.Year
            });

            var linesCount = 0;

            while (linesCount < recordCount)
            {
                stopToken.ThrowIfCancellationRequested();
                var(line, consumed) = await reader.ReadAsync(stopToken);

                _logger.LogTrace("File: '{0}', line: '{1}', bytes: {2}", context.FilePath, line, consumed);
                context.Position += consumed;
                if (line is null)
                {
                    break;
                }
                context.LineNumber++;

                var valid = parser.ParseString(line, ref packet);
                if (!valid)
                {
                    _logger.LogWarning($"Unable to parse record at line {context.LineNumber} in file {context.FilePath}. Record may be in invalid format");
                }
                var record = new SyslogData(
                    packet.TimeStamp ?? DateTimeOffset.Now,
                    packet.HostName,
                    packet.Tag,
                    packet.Content);

                var envelope = new LogEnvelope <SyslogData>(
                    record,
                    record.Timestamp.UtcDateTime,
                    line,
                    context.FilePath,
                    context.Position,
                    context.LineNumber);

                output.Add(envelope);
                linesCount++;
            }
        }
Esempio n. 2
0
        private IEnumerable <IEnvelope <SyslogData> > ParseRfc3164Log(StreamReader sr, LogContext context)
        {
            var packet = new Rfc3164Packet();
            var parser = new Rfc3164Parser(new Rfc3164ParserOptions
            {
                RequirePri  = false,
                DefaultYear = DateTime.Now.Year
            });

            while (!sr.EndOfStream)
            {
                // TODO change the ReadLine() functionality to using the full-line reader
                var line = sr.ReadLine();
                context.LineNumber++;
                if (!string.IsNullOrWhiteSpace(line))
                {
                    var valid = parser.ParseString(line, ref packet);
                    if (!valid)
                    {
                        _logger.LogWarning($"Unable to parse record at line {context.LineNumber} in file {context.FilePath}. Record may be in invalid format");
                    }
                    var record = new SyslogData(
                        packet.TimeStamp ?? DateTimeOffset.Now,
                        packet.HostName,
                        packet.Tag,
                        packet.Content);

                    yield return(new LogEnvelope <SyslogData>(record,
                                                              record.Timestamp.UtcDateTime,
                                                              line,
                                                              context.FilePath,
                                                              context.Position,
                                                              context.LineNumber));
                }
            }
        }