1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Collections; 6 7 namespace System.Diagnostics 8 { 9 public class EventLogEntryCollection : ICollection 10 { 11 private readonly EventLogInternal _log; 12 EventLogEntryCollection(EventLogInternal log)13 internal EventLogEntryCollection(EventLogInternal log) 14 { 15 _log = log; 16 } 17 18 public int Count 19 { 20 get 21 { 22 return _log.EntryCount; 23 } 24 } 25 26 public virtual EventLogEntry this[int index] 27 { 28 get 29 { 30 return _log.GetEntryAt(index); 31 } 32 } 33 CopyTo(EventLogEntry[] entries, int index)34 public void CopyTo(EventLogEntry[] entries, int index) 35 { 36 ((ICollection)this).CopyTo((Array)entries, index); 37 } 38 GetEnumerator()39 public IEnumerator GetEnumerator() 40 { 41 return new EntriesEnumerator(this); 42 } 43 GetEntryAtNoThrow(int index)44 internal EventLogEntry GetEntryAtNoThrow(int index) 45 { 46 return _log.GetEntryAtNoThrow(index); 47 } 48 49 bool ICollection.IsSynchronized 50 { 51 get 52 { 53 return false; 54 } 55 } 56 57 object ICollection.SyncRoot 58 { 59 get 60 { 61 return this; 62 } 63 } 64 ICollection.CopyTo(Array array, int index)65 void ICollection.CopyTo(Array array, int index) 66 { 67 EventLogEntry[] entries = _log.GetAllEntries(); 68 Array.Copy(entries, 0, array, index, entries.Length); 69 } 70 71 private class EntriesEnumerator : IEnumerator 72 { 73 private EventLogEntryCollection entries; 74 private int num = -1; 75 private EventLogEntry cachedEntry = null; 76 EntriesEnumerator(EventLogEntryCollection entries)77 internal EntriesEnumerator(EventLogEntryCollection entries) 78 { 79 this.entries = entries; 80 } 81 82 public object Current 83 { 84 get 85 { 86 if (cachedEntry == null) 87 throw new InvalidOperationException(SR.NoCurrentEntry); 88 89 return cachedEntry; 90 } 91 } 92 MoveNext()93 public bool MoveNext() 94 { 95 num++; 96 cachedEntry = entries.GetEntryAtNoThrow(num); 97 return cachedEntry != null; 98 } 99 Reset()100 public void Reset() 101 { 102 num = -1; 103 } 104 } 105 } 106 } 107