Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

EventLogEntryCollection Class

Defines size and enumerators for a collection of EventLogEntry instances.

For a list of all members of this type, see EventLogEntryCollection Members.

System.Object
   System.Diagnostics.EventLogEntryCollection

[Visual Basic]
Public Class EventLogEntryCollection
   Implements ICollection, IEnumerable
[C#]
public class EventLogEntryCollection : ICollection, IEnumerable
[C++]
public __gc class EventLogEntryCollection : public ICollection,
   IEnumerable
[JScript]
public class EventLogEntryCollection implements ICollection,
   IEnumerable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Use the EventLogEntryCollection class when reading the entries associated with an EventLog instance. The EventLog class's Entries property is a collection of all the entries in the event log.

Because new entries are appended to the end of the existing list, stepping through the collection enables you access to entries created after you originally created the EventLogEntryCollection. However, once you view the entire list, it is not updated with new entries.

Example

[Visual Basic] 
Imports System
Imports System.Collections
Imports System.Diagnostics

Class EventLogEntryCollection_Item
   Public Shared Sub Main()
      Try
         Dim myLogName As String = "MyNewlog"
         ' Check if the source exists.
         If Not EventLog.SourceExists("MySource") Then
            'Create source.
            EventLog.CreateEventSource("MySource", myLogName)
            Console.WriteLine("Creating EventSource")
         ' Get the EventLog associated if the source exists.
         Else
            myLogName = EventLog.LogNameFromSourceName("MySource", ".")
         End If
         ' Create an EventLog instance and assign its source.
         Dim myEventLog2 As New EventLog()
         myEventLog2.Source = "MySource"
         ' Write an informational entry to the event log.
         myEventLog2.WriteEntry("Successfully created a new Entry in the Log")
         myEventLog2.Close()
         ' Create a new EventLog object.
         Dim myEventLog1 As New EventLog()
         myEventLog1.Log = myLogName

         ' Obtain the Log Entries of "MyNewLog".
         Dim myEventLogEntryCollection As EventLogEntryCollection = myEventLog1.Entries
         myEventLog1.Close()
         Console.WriteLine("The number of entries in 'MyNewLog' = " + _
                           myEventLogEntryCollection.Count.ToString())

         ' Display the 'Message' property of EventLogEntry.
         Dim i As Integer
         For i = 0 To myEventLogEntryCollection.Count - 1
            Console.WriteLine("The Message of the EventLog is :" + _
                              myEventLogEntryCollection(i).Message)
         Next i
         ' Copy the EventLog entries to Array of type EventLogEntry.
         Dim myEventLogEntryArray(myEventLogEntryCollection.Count-1) As EventLogEntry
         myEventLogEntryCollection.CopyTo(myEventLogEntryArray, 0)
         Dim myEnumerator As IEnumerator = myEventLogEntryArray.GetEnumerator()
         While myEnumerator.MoveNext()
            Dim myEventLogEntry As EventLogEntry = CType(myEnumerator.Current, EventLogEntry)
            Console.WriteLine("The LocalTime the Event is generated is " + _
                                 myEventLogEntry.TimeGenerated)
         End While
      Catch e As Exception
         Console.WriteLine("Exception:{0}", e.Message.ToString())
      End Try
   End Sub 'Main
End Class 'EventLogEntryCollection_Item

[C#] 
using System;
using System.Collections;
using System.Diagnostics;

class EventLogEntryCollection_Item
{
   public static void Main()
   {
      try
      {
         string myLogName = "MyNewLog";
         // Check if the source exists.
         if(!EventLog.SourceExists("MySource"))
         {
            //Create source.
            EventLog.CreateEventSource("MySource", myLogName);
            Console.WriteLine("Creating EventSource");
         }
         else
            // Get the EventLog associated if the source exists.
            myLogName = EventLog.LogNameFromSourceName("MySource",".");

         // Create an EventLog instance and assign its source.
         EventLog myEventLog2 = new EventLog();
         myEventLog2.Source = "MySource";
         // Write an informational entry to the event log.
         myEventLog2.WriteEntry("Successfully created a new Entry in the Log");
         myEventLog2.Close();
         // Create a new EventLog object.
         EventLog myEventLog1 = new EventLog();
         myEventLog1.Log = myLogName;

         // Obtain the Log Entries of "MyNewLog".
         EventLogEntryCollection myEventLogEntryCollection=
            myEventLog1.Entries;
         myEventLog1.Close();
         Console.WriteLine("The number of entries in 'MyNewLog' = "
            +myEventLogEntryCollection.Count);

         // Display the 'Message' property of EventLogEntry.
         for(int i=0;i<myEventLogEntryCollection.Count;i++)
         {
            Console.WriteLine("The Message of the EventLog is :"
               +myEventLogEntryCollection[i].Message);
         }

         // Copy the EventLog entries to Array of type EventLogEntry.
         EventLogEntry[] myEventLogEntryArray= 
            new EventLogEntry[myEventLogEntryCollection.Count];
         myEventLogEntryCollection.CopyTo(myEventLogEntryArray,0);
         IEnumerator myEnumerator=myEventLogEntryArray.GetEnumerator();
         while(myEnumerator.MoveNext())
         {
            EventLogEntry myEventLogEntry =(EventLogEntry)myEnumerator.Current;
            Console.WriteLine("The LocalTime the Event is generated is "
               +myEventLogEntry.TimeGenerated);
         }
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception:{0}",e.Message);
      }
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Diagnostics;

int main() {
    try {
        String* myLogName = S"MyNewLog";
        // Check if the source exists.
        if (!EventLog::SourceExists(S"MySource")) {
            //Create source.
            EventLog::CreateEventSource(S"MySource", myLogName);
            Console::WriteLine(S"Creating EventSource");
        } else
            // Get the EventLog associated if the source exists.
            myLogName = EventLog::LogNameFromSourceName(S"MySource", S".");

        // Create an EventLog instance and assign its source.
        EventLog* myEventLog2 = new EventLog();
        myEventLog2->Source = S"MySource";
        // Write an informational entry to the event log.
        myEventLog2->WriteEntry(S"Successfully created a new Entry in the Log");
        myEventLog2->Close();
        // Create a new EventLog Object*.
        EventLog* myEventLog1 = new EventLog();
        myEventLog1->Log = myLogName;

        // Obtain the Log Entries of S"MyNewLog".
        EventLogEntryCollection* myEventLogEntryCollection =
            myEventLog1->Entries;
        myEventLog1->Close();
        Console::WriteLine(S"The number of entries in 'MyNewLog' = {0}", 
            __box(myEventLogEntryCollection->Count));

        // Display the 'Message' property of EventLogEntry.
        for (int i=0;i<myEventLogEntryCollection->Count;i++) {
            Console::WriteLine(S"The Message of the EventLog is : {0}", 
                myEventLogEntryCollection->Item[i]->Message);
        }

        // Copy the EventLog entries to Array of type EventLogEntry.
        EventLogEntry* myEventLogEntryArray[] = 
            new EventLogEntry*[myEventLogEntryCollection->Count];
        myEventLogEntryCollection->CopyTo(myEventLogEntryArray, 0);
        IEnumerator* myEnumerator=myEventLogEntryArray->GetEnumerator();
        while (myEnumerator->MoveNext()) {
            EventLogEntry* myEventLogEntry =
                __try_cast<EventLogEntry*>(myEnumerator->Current);
            Console::WriteLine(S"The LocalTime the Event is generated is {0}", 
                __box(myEventLogEntry->TimeGenerated));
        }
    } catch (Exception* e) {
        Console::WriteLine(S"Exception: {0}", e->Message);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Diagnostics

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

EventLogEntryCollection Members | System.Diagnostics Namespace | EventLog | Entries

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.