Beispiel #1
0
        /// <summary>
        /// Parses the specified log lines and fires an OnLineParsed event for each line parsed.
        /// </summary>
        /// <param name="logLines">Lines of text from the log.</param>
        /// <returns>A list of LogEntry objects containing data from the parsed log lines.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when the logLines argument is null.</exception>
        public override IList <LogEntry> Parse(IList <string> logLines)
        {
            if (logLines == null)
            {
                throw new ArgumentNullException("logLines");
            }

            List <LogEntry> list          = new List <LogEntry>();
            int             startingIndex = Extras.FindStartingIndex(Log, logLines);

            for (int i = startingIndex; i < logLines.Count; i++)
            {
                if (string.IsNullOrEmpty(logLines[i]))
                {
                    continue;
                }

                // parse out date/time and message parts of the log
                MatchCollection matches = Regex.Matches(logLines[i], @"^(.+?\ .+?)\ (.+?)$");
                if (matches.Count <= 0)
                {
                    continue;
                }

                // only capture entries that match the regular expression and add them to the LogEntry list.
                foreach (Match match in matches)
                {
                    if (match.Groups.Count > 0)
                    {
                        LogEntry logEntry = new LogEntry();
                        logEntry.Index     = i;
                        logEntry.Message   = match.Groups[2].Value;
                        logEntry.Source    = Log;
                        logEntry.Timestamp = DateTime.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);

                        list.Add(logEntry);
                        this.OnLineParsed(this, new LogEntryItemEventArgs(logEntry, logLines[i]));
                    }
                }
            }

            // capture data thats useful on subsequent runs over the same log file.
            Log.LastLineCount  = logLines.Count;
            Log.LastParsedLine = logLines[startingIndex];

            return(list);
        }
Beispiel #2
0
        /// <summary>
        /// Parses the specified log lines.
        /// </summary>
        /// <param name="logLines">The log lines.</param>
        /// <returns>A list of zero, or more, LogEntry objects.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when the logLines argument is null.</exception>
        public override IList <LogEntry> Parse(IList <string> logLines)
        {
            if (logLines == null)
            {
                throw new ArgumentNullException("logLines");
            }

            if (logLines.Count == 0)
            {
                return(new List <LogEntry>());
            }

            // rearrange the log so that the oldest entries are at the top of the list
            var lineArray = new string[logLines.Count];

            logLines.CopyTo(lineArray, 0);
            var lines = new List <string>(NormalizeLog(string.Join("\r\n", lineArray).Replace("\r\n\t", " ").Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)));

            var list          = new List <LogEntry>();
            int startingIndex = Extras.FindStartingIndex(Log, lines);

            var expectedRex = ExpectedRex.New;

            for (int i = startingIndex; i < lines.Count; i++)
            {
                if (string.IsNullOrEmpty(lines[i].Trim()))
                {
                    continue;
                }

                // New - create (reinitialize) new log entry object
                if (expectedRex == ExpectedRex.New)
                {
                    _logEntry = new LogEntry();
                }

                // Complete - add log entry to list and setup for new log entry
                if (expectedRex == ExpectedRex.Complete)
                {
                    if (_extensions.Length > 0)
                    {
                        _extensions.Append("}");
                        _logEntry.Extensions = _extensions.ToString();
                        _extensions          = new StringBuilder();
                    }

                    // White space at the beginning of the line? Not quite done yet...
                    var regex = new Regex(@"^\s");
                    if (regex.IsMatch(lines[i]))
                    {
                        expectedRex = ExpectedRex.Message2;
                    }
                    else
                    {
                        list.Add(_logEntry);
                        expectedRex = ExpectedRex.New;
                    }

                    this.OnLineParsed(this, new LogEntryItemEventArgs(_logEntry, lines[i]));
                }

                // continue parsing until ExpectedRex.Complete
                expectedRex = ParseLine(lines[i], expectedRex);
            }

            // the last log entry falls out of the for loop before it's
            // saved...so grab any extension data, post it to the log entry
            // and save the log entry to the list.
            if (_extensions.Length > 0)
            {
                _extensions.Append("}");
                _logEntry.Extensions = _extensions.ToString();
            }

            list.Add(_logEntry);

            return(list);
        }