Exemplo n.º 1
0
        public static FilterAll All(IEnumerable <Filter> filters)
        {
            var result = new FilterAll();

            result.AddRange(filters);

            return(result);
        }
Exemplo n.º 2
0
        private static void ParseXml(string filename)
        {
            using (new TimeTrace ("total"))
            {
                using (new TimeTrace ("process with E/S init"))
                {
                    using (var eventSource = Log4JFile.Create (filename))
                    using (new TimeTrace ("process inner"))
                    using (var filterTs = new FilterTimestamp (1411231371536L, 1411231371556L))
                    using (var filterLvl = new FilterLevel ("INFO", "ERROR"))
                    using (var filterLgr = new FilterLogger ("Root.ChildB"))
                    using (var filterMsg1 = new FilterMessage ("#2"))
                    using (var filterMsg2 = new FilterMessage ("#3"))
                    using (var filterNot = new FilterNot (filterLvl))
                    using (var filterAny = new FilterAny ())
                    using (var filterAll = new FilterAll ())
                    {
                        filterAny.Add (filterMsg1);
                        filterAny.Add (filterMsg2);

                        filterAll.Add (filterTs);
                        filterAll.Add (filterNot);
                        filterAll.Add (filterAny);
                        filterAll.Add (filterLgr);

                        var matchingEvents = eventSource
                            .GetEvents ()
                            .Where (filterAll)
                            .Take (20)
                            .ToList ();

                        foreach (var @event in matchingEvents)
                        {
                            PrintEvent (@event);
                        }

                        Console.WriteLine ("Found events: {0}", matchingEvents.Count);
                    }
                }

                using (new TimeTrace ("count all events"))
                using (var eventSource = Log4JFile.Create (filename))
                {
                    var count = eventSource.GetEvents ().Count ();

                    Console.WriteLine ("Found events: {0}", count);
                }
            }
        }
Exemplo n.º 3
0
        public EventsCollection GetEvents(LogQuery query)
        {
            var sourceModel = query.Source.Value;
            var source = logSourceProvider_.GetSource (sourceModel?.Id);

            using (var logFile = source.Open (sourceModel?.Size))
            using (var filters = new List<FilterBase> ().ToDisposable ())
            {
                if (query.MinLevel.Value != Level.Debug)
                {
                    AddFilter (filters, () => new FilterLevel (query.MinLevel.Value, Level.MaxValue));
                }

                if (!String.IsNullOrWhiteSpace (query.Logger))
                {
                    AddFilter (filters, () => new FilterLogger (query.Logger));
                }

                if (!String.IsNullOrWhiteSpace (query.Message))
                {
                    AddFilter (filters, () => new FilterMessage (query.Message));
                }

                if (query.MinTime > DateTime.MinValue || query.MaxTime < DateTime.MaxValue)
                {
                    AddFilter (filters, () => new FilterTimestamp (query.MinTime, query.MaxTime));
                }

                IEnumerable<Event> filteredEvents;
                switch (filters.Elements.Count)
                {
                    case 0:
                        filteredEvents = logFile.GetEventsReverse ();
                        break;

                    case 1:
                        filteredEvents = logFile
                            .GetEventsReverse ()
                            .Where (filters.Elements.Single ());
                        break;

                    default:
                        FilterAll rootFilter = null;
                        try
                        {
                            rootFilter = new FilterAll ();
                            foreach (var filter in filters.Elements)
                            {
                                rootFilter.Add (filter);
                            }
                        }
                        finally
                        {
                            if (rootFilter != null)
                            {
                                filters.Elements.Add (rootFilter);
                            }
                        }
                        filteredEvents = logFile
                            .GetEventsReverse ()
                            .Where (rootFilter);
                        break;
                }

                var eventsWindow = filteredEvents
                    .Skip (query.Offset)
                    .Take (query.Quantity);

                var events = eventsWindow
                    .Select (x => new EventModel (x))
                    .ToList ();
                events.Reverse ();
                var sourceName = source.Name;
                var sourceSize = logFile.Size;
                return new EventsCollection (events, sourceName, sourceSize);
            }
        }