Ejemplo n.º 1
0
        public IEnumerable<GrepResult> Grep(string subject, string expression, bool caseInsensitive)
        {
            Regex matcher = new Regex("(" + expression + ")", RegexOptions.Compiled | (caseInsensitive ? RegexOptions.IgnoreCase : RegexOptions.None));
            var messagePropertyFilter = new MessagePropertyFilter
                                            {
                                                ArrivedTime = true,
                                                Body = true,
                                                Id = true,
                                                Label = true
                                            };

            var messageQueue = new MessageQueue(subject);
            messageQueue.MessageReadPropertyFilter = messagePropertyFilter;

            var messageEnumerator2 = messageQueue.GetMessageEnumerator2();
            while (messageEnumerator2.MoveNext())
            {
                if (messageEnumerator2.Current == null)
                    continue;

                var message = messageEnumerator2.Current;
                using (var streamReader = new StreamReader(message.BodyStream))
                {
                    string body = streamReader.ReadToEnd();
                    if (!matcher.IsMatch(body))
                        continue;

                    yield return
                        new GrepResult
                            {Message = CreateMessageDescriptor(message), Text = matcher.HighlightMatch(body, "<{0}>")};
                }
            }
        }