private EventLogWatcher CreateWatcher()
        {
            EventBookmark eventBookmark = this.Bookmarker.Read();

            if (eventBookmark != null)
            {
                this.Trace("Previous bookmark found", new object[0]);
            }
            else
            {
                this.Trace("No previous book mark found at the first attempt", new object[0]);
                eventBookmark = this.SetBookmarkForLatestFailureItem();
                if (eventBookmark != null)
                {
                    this.Trace("Updated the bookmark with the last failure item raised", new object[0]);
                }
                else
                {
                    this.Trace("There is no bookmark since there are no failure items in the channel", new object[0]);
                }
            }
            bool            readExistingEvents = eventBookmark != null;
            EventLogQuery   eventQuery         = FailureItemWatcher.CreateEventLogQueryByDatabaseGuid(this.m_database.Guid);
            EventLogWatcher eventLogWatcher    = new EventLogWatcher(eventQuery, eventBookmark, readExistingEvents);

            eventLogWatcher.EventRecordWritten += this.EventArrivedHandler;
            return(eventLogWatcher);
        }
        private EventBookmark SetBookmarkForLatestFailureItem()
        {
            EventBookmark eventBookmark = null;
            EventLogQuery eventLogQuery = FailureItemWatcher.CreateEventLogQueryByDatabaseGuid(this.m_database.Guid);

            eventLogQuery.ReverseDirection = true;
            using (EventLogReader eventLogReader = new EventLogReader(eventLogQuery))
            {
                using (EventRecord eventRecord = eventLogReader.ReadEvent())
                {
                    if (eventRecord != null)
                    {
                        DatabaseFailureItem databaseFailureItem = DatabaseFailureItem.Parse(eventRecord);
                        eventBookmark = databaseFailureItem.Bookmark;
                        this.Bookmarker.Write(eventBookmark);
                    }
                }
            }
            return(eventBookmark);
        }
        internal static DatabaseFailureItem FindMostRecentIdenticalFailureItem(DatabaseFailureItem referenceDbfi, out int countDupliateItems, out EventRecord latestRecord)
        {
            EventLogQuery       eventQuery = FailureItemWatcher.CreateEventLogQueryByDatabaseGuid(referenceDbfi.Guid);
            DatabaseFailureItem result;

            using (EventLogReader eventLogReader = new EventLogReader(eventQuery, referenceDbfi.Bookmark))
            {
                latestRecord = null;
                DatabaseFailureItem databaseFailureItem = null;
                countDupliateItems = 0;
                for (;;)
                {
                    EventRecord eventRecord2;
                    EventRecord eventRecord = eventRecord2 = eventLogReader.ReadEvent();
                    try
                    {
                        if (eventRecord != null)
                        {
                            DatabaseFailureItem databaseFailureItem2 = DatabaseFailureItem.Parse(eventRecord);
                            if (referenceDbfi.Equals(databaseFailureItem2))
                            {
                                databaseFailureItem = databaseFailureItem2;
                                latestRecord        = eventRecord;
                                countDupliateItems++;
                                continue;
                            }
                        }
                    }
                    finally
                    {
                        if (eventRecord2 != null)
                        {
                            ((IDisposable)eventRecord2).Dispose();
                        }
                    }
                    break;
                }
                result = databaseFailureItem;
            }
            return(result);
        }