EventLogEntryCollection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.Diagnostics.EventLogEntryCollection.cs
  3. //
  4. // Authors:
  5. // Jonathan Pryor ([email protected])
  6. //
  7. // (C) 2002 Jonathan Pryor
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Diagnostics;
  32. namespace System.Diagnostics {
  33. public class EventLogEntryCollection : ICollection, IEnumerable {
  34. readonly EventLogImpl _impl;
  35. internal EventLogEntryCollection(EventLogImpl impl)
  36. {
  37. _impl = impl;
  38. }
  39. public int Count {
  40. get { return _impl.EntryCount; }
  41. }
  42. public virtual EventLogEntry this [int index] {
  43. get { return _impl[index]; }
  44. }
  45. bool ICollection.IsSynchronized {
  46. get { return false; }
  47. }
  48. object ICollection.SyncRoot {
  49. get { return this; }
  50. }
  51. public void CopyTo (EventLogEntry[] eventLogEntries, int index)
  52. {
  53. EventLogEntry[] entries = _impl.GetEntries ();
  54. Array.Copy (entries, 0, eventLogEntries, index, entries.Length);
  55. }
  56. public IEnumerator GetEnumerator ()
  57. {
  58. return new EventLogEntryEnumerator (_impl);
  59. }
  60. void ICollection.CopyTo (Array array, int index)
  61. {
  62. EventLogEntry[] entries = _impl.GetEntries ();
  63. Array.Copy (entries, 0, array, index, entries.Length);
  64. }
  65. private class EventLogEntryEnumerator : IEnumerator
  66. {
  67. internal EventLogEntryEnumerator (EventLogImpl impl)
  68. {
  69. _impl = impl;
  70. }
  71. object IEnumerator.Current {
  72. get { return Current; }
  73. }
  74. public EventLogEntry Current {
  75. get {
  76. #if NET_2_0
  77. if (_currentEntry != null)
  78. return _currentEntry;
  79. #else
  80. if (_currentIndex >= 0 && _currentIndex < _impl.EntryCount)
  81. return _impl [_currentIndex];
  82. #endif
  83. throw new InvalidOperationException ("No current EventLog"
  84. + " entry available, cursor is located before the first"
  85. + " or after the last element of the enumeration.");
  86. }
  87. }
  88. public bool MoveNext ()
  89. {
  90. _currentIndex++;
  91. #if NET_2_0
  92. if (_currentIndex >= _impl.EntryCount) {
  93. _currentEntry = null;
  94. return false;
  95. }
  96. _currentEntry = _impl [_currentIndex];
  97. return true;
  98. #else
  99. return (_currentIndex < _impl.EntryCount);
  100. #endif
  101. }
  102. public void Reset ()
  103. {
  104. _currentIndex = - 1;
  105. }
  106. readonly EventLogImpl _impl;
  107. int _currentIndex = -1;
  108. #if NET_2_0
  109. EventLogEntry _currentEntry;
  110. #endif
  111. }
  112. }
  113. }