EventLogEntryCollection 类

定义 EventLogEntry 实例集合的大小和枚举数。

**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)

语法

声明
Public Class EventLogEntryCollection
    Implements ICollection, IEnumerable
用法
Dim instance As EventLogEntryCollection
public class EventLogEntryCollection : ICollection, IEnumerable
public ref class EventLogEntryCollection : ICollection, IEnumerable
public class EventLogEntryCollection implements ICollection, IEnumerable
public class EventLogEntryCollection implements ICollection, IEnumerable

备注

在读取与 EventLog 实例关联的项时,使用 EventLogEntryCollection 类。EventLog 类的 Entries 属性是事件日志中所有项的集合。

由于新项被追加到现有列表的末尾,因此逐句通过该集合使您能够访问在最初创建 EventLogEntryCollection 之后创建的项。但是,一旦查看整个列表,该列表就不会用新项进行更新。

示例

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
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);
      }
   }
}
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Diagnostics;
int 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
            myLogName = EventLog::LogNameFromSourceName( "MySource", "." );
      
      // Get the EventLog associated if the source exists.
      // Create an EventLog instance and assign its source.
      EventLog^ myEventLog2 = gcnew 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 = gcnew EventLog;
      myEventLog1->Log = myLogName;
      
      // Obtain the Log Entries of S"MyNewLog".
      EventLogEntryCollection^ myEventLogEntryCollection = myEventLog1->Entries;
      myEventLog1->Close();
      Console::WriteLine( "The number of entries in 'MyNewLog' = {0}", myEventLogEntryCollection->Count );
      
      // Display the 'Message' property of EventLogEntry.
      for ( int i = 0; i < myEventLogEntryCollection->Count; i++ )
      {
         Console::WriteLine( "The Message of the EventLog is : {0}", myEventLogEntryCollection[ i ]->Message );
      }
      
      // Copy the EventLog entries to Array of type EventLogEntry.
      array<EventLogEntry^>^myEventLogEntryArray = gcnew array<EventLogEntry^>(myEventLogEntryCollection->Count);
      myEventLogEntryCollection->CopyTo( myEventLogEntryArray, 0 );
      IEnumerator^ myEnumerator = myEventLogEntryArray->GetEnumerator();
      while ( myEnumerator->MoveNext() )
      {
         EventLogEntry^ myEventLogEntry = safe_cast<EventLogEntry^>(myEnumerator->Current);
         Console::WriteLine( "The LocalTime the Event is generated is {0}", myEventLogEntry->TimeGenerated );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
import System.*;
import System.Collections.*;
import System.Diagnostics.*;

class EventLogEntryCollectionItem
{
    public static void main(String[] args)
    {
        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.set_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.set_Log(myLogName);

            // Obtain the Log Entries of "MyNewLog".
            EventLogEntryCollection myEventLogEntryCollection = 
                myEventLog1.get_Entries();

            myEventLog1.Close();
            Console.WriteLine("The number of entries in 'MyNewLog' = " 
                + myEventLogEntryCollection.get_Count());

            // Display the 'Message' property of EventLogEntry.
            for (int i = 0; i < myEventLogEntryCollection.get_Count(); i++) {
                Console.WriteLine("The Message of the EventLog is :"
                    + myEventLogEntryCollection.get_Item(i).get_Message());
            }
            // Copy the EventLog entries to Array of type EventLogEntry.
            EventLogEntry myEventLogEntryArray[] = 
                new EventLogEntry[myEventLogEntryCollection.get_Count()];

            myEventLogEntryCollection.CopyTo(myEventLogEntryArray, 0);
            IEnumerator myEnumerator = myEventLogEntryArray.GetEnumerator();
            while (myEnumerator.MoveNext()) {
                EventLogEntry myEventLogEntry = 
                    (EventLogEntry)myEnumerator.get_Current();
                Console.WriteLine("The LocalTime the Event is generated is " 
                    + myEventLogEntry.get_TimeGenerated());
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception:{0}", e.get_Message());
        }
    } //main
} //EventLogEntryCollectionItem

继承层次结构

System.Object
  System.Diagnostics.EventLogEntryCollection

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

EventLogEntryCollection 成员
System.Diagnostics 命名空间
EventLog 类
EventLog.Entries 属性