Example #1
0
        public List <Message> Parse()
        {
            List <ValidationError> errors = null;

            //validate target
            if (!new ParserValidator().Validate(Target, out errors))
            {
                if (errors != null && errors.Count > 0)
                {
                    ErrorsStack.AddErrors(errors);
                }
                return(null);
            }

            var  format            = Target.Format.FullFormat;
            var  portions          = Regex.Matches(format, @"\[[A-Z][a-z]*\]").Cast <Match>().Select(x => x.Value).ToArray();
            var  message           = new Message();
            var  messages          = new List <Message>();
            bool isMessageSpanning = false;

            //parse messages
            for (int lineIndex = 0; lineIndex < Target.PlainLines.Count; lineIndex++)
            {
                if (!isMessageSpanning)
                {
                    message = new Message();
                    for (int portionIndex = 0; portionIndex < portions.Length; portionIndex++)
                    {
                        List <PlainLine> spanningLines = null;
                        isMessageSpanning = IsCurrentMessageSpanning(lineIndex, out spanningLines) ?? false;
                        if (portions[portionIndex] == AppResources.Parser_Format_Stamp)
                        {
                            message.TimeStamp = GetTimeStamp(Target.PlainLines[lineIndex]);
                        }
                        if (portions[portionIndex] == AppResources.Parser_Format_Type)
                        {
                            message.Type = GetMessageType(Target.PlainLines[lineIndex]) ?? LogType.Undefined;
                        }
                        if (portions[portionIndex] == AppResources.Parser_Format_Message)
                        {
                            message.MessageBuilder = GetMessageBuilder(Target.PlainLines[lineIndex], spanningLines);
                        }
                    }
                    messages.Add(message);
                }

                //TODO: find a way to detect spanning messages
                //TODO: parse spanned messages by adding it to MessageBuilder
            }
            return(messages);
        }
Example #2
0
        public List <Message> Filter()
        {
            List <ValidationError> errors = null;

            //validate target
            if (!new FilterValidator().Validate(Target, out errors))
            {
                if (errors != null && errors.Count > 0)
                {
                    ErrorsStack.AddErrors(errors);
                }
                return(null);
            }

            #region prepare

            var target = this.Target as FilterObject;

            var messagesFiltered = target.Messages;

            #endregion

            #region filtration

            if (target.SelectedTypes != null && target.SelectedTypes.Count > 0)
            {
                messagesFiltered = messagesFiltered.Where(x => target.SelectedTypes.Contains(x.Type)).ToList();
            }
            if (target.SelectedTimeStamps != null && target.SelectedTimeStamps.Count > 0)
            {
                var dateStamps = target.SelectedTimeStamps.Select(x => x.Date);
                messagesFiltered = messagesFiltered.Where(x => x.TimeStamp.HasValue ? dateStamps.Contains(x.TimeStamp.Value.Date) : false).ToList();
            }
            if (target.SelectedRange != null)
            {
                messagesFiltered = messagesFiltered.Where(x => x.TimeStamp.HasValue ?
                                                          x.TimeStamp.Value >= target.SelectedRange.From &&
                                                          x.TimeStamp.Value < target.SelectedRange.To : false).ToList();
            }
            if (target.MessageCriteria != null)
            {
                messagesFiltered = messagesFiltered.Where(x => x.TextMessage.Contains(target.MessageCriteria)).ToList();
            }

            #endregion

            return(messagesFiltered);
        }
Example #3
0
        public List <PlainLine> Read()
        {
            List <ValidationError> errors = null;

            //validate target
            if (!new ReaderValidator().Validate(Target, out errors))
            {
                if (errors != null && errors.Count > 0)
                {
                    ErrorsStack.AddErrors(errors);
                }
                return(null);
            }
            var target = this.Target as ReaderObject;
            //process target
            FileStream       fs    = new FileStream(target.FileName, FileMode.Open);
            StreamReader     sr    = new StreamReader(fs);
            List <PlainLine> lines = new List <PlainLine>();
            int lineIndex          = 0;

            while (!sr.EndOfStream)
            {
                if (target.PageSize.HasValue && target.PageIndex.HasValue)
                {
                    if (lineIndex >= (target.PageSize.Value * target.PageIndex.Value) &&
                        lineIndex < target.PageSize.Value + (target.PageSize.Value * target.PageIndex.Value))
                    {
                        lines.Add(new PlainLine(sr.ReadLine()));
                    }
                    else
                    {
                        var dump = sr.ReadLine(); // to skip this line
                    }
                    if (lines.Count == target.PageSize.Value)
                    {
                        break;
                    }
                }
                else
                {
                    lines.Add(new PlainLine(sr.ReadLine()));
                }
                lineIndex++;
            }
            return(lines);
        }