Bagikan melalui


EventLog.GetEventLogs Metode

Definisi

Membuat array log peristiwa.

Overload

GetEventLogs(String)

Mencari semua log peristiwa di komputer yang diberikan dan membuat array EventLog objek yang berisi daftar.

GetEventLogs()

Mencari semua log peristiwa di komputer lokal dan membuat array EventLog objek yang berisi daftar.

GetEventLogs(String)

Sumber:
EventLog.cs
Sumber:
EventLog.cs
Sumber:
EventLog.cs

Mencari semua log peristiwa di komputer yang diberikan dan membuat array EventLog objek yang berisi daftar.

public:
 static cli::array <System::Diagnostics::EventLog ^> ^ GetEventLogs(System::String ^ machineName);
public static System.Diagnostics.EventLog[] GetEventLogs (string machineName);
static member GetEventLogs : string -> System.Diagnostics.EventLog[]
Public Shared Function GetEventLogs (machineName As String) As EventLog()

Parameter

machineName
String

Komputer tempat mencari log peristiwa.

Mengembalikan

Array jenis EventLog yang mewakili log pada komputer tertentu.

Pengecualian

Parameter machineName adalah nama komputer yang tidak valid.

Anda tidak memiliki akses baca ke registri.

-atau-

Tidak ada layanan log peristiwa pada komputer.

Contoh

Contoh berikut mendapatkan daftar log di komputer "myServer". Kemudian menghasilkan nama setiap log.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   array<EventLog^>^remoteEventLogs;
   remoteEventLogs = EventLog::GetEventLogs( "myServer" );
   Console::WriteLine( "Number of logs on computer: {0}", remoteEventLogs->Length );
   System::Collections::IEnumerator^ myEnum = remoteEventLogs->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      EventLog^ log = safe_cast<EventLog^>(myEnum->Current);
      Console::WriteLine( "Log: {0}", log->Log );
   }
}
using System;
using System.Diagnostics;

class MySample
{
    public static void Main()
    {
        EventLog[] remoteEventLogs;

        remoteEventLogs = EventLog.GetEventLogs("myServer");

        Console.WriteLine("Number of logs on computer: " + remoteEventLogs.Length);

        foreach (EventLog log in remoteEventLogs)
        {
            Console.WriteLine("Log: " + log.Log);
        }
    }
}
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        Dim remoteEventLogs() As EventLog
        
        remoteEventLogs = EventLog.GetEventLogs("myServer")
        
        Console.WriteLine(("Number of logs on computer: " & remoteEventLogs.Length))
        
        Dim log As EventLog
        For Each log In  remoteEventLogs
            Console.WriteLine(("Log: " & log.Log))
        Next log
    End Sub
End Class

Keterangan

Array EventLog objek adalah rekam jepret dari semua log peristiwa di komputer yang ditentukan oleh machineName parameter saat panggilan ke GetEventLogs dilakukan. Ini bukan koleksi dinamis, sehingga tidak mencerminkan penghapusan atau pembuatan log secara real time. Anda harus memverifikasi bahwa log dalam array ada sebelum Anda membaca atau menulisnya. Array biasanya mencakup setidaknya tiga log: Aplikasi, Sistem, dan Keamanan. Jika Anda membuat log kustom di komputer yang ditentukan, log tersebut juga akan muncul dalam array.

GetEventLogs adalah static metode , sehingga dapat dipanggil pada kelas itu EventLog sendiri. Tidak perlu membuat instans EventLog objek untuk melakukan panggilan ke metode .

Untuk mengambil daftar log peristiwa, Anda harus memiliki izin registri yang sesuai. Izin ini identik dengan yang diperlukan untuk memanggil Exists dan SourceExists.

Lihat juga

Berlaku untuk

GetEventLogs()

Sumber:
EventLog.cs
Sumber:
EventLog.cs
Sumber:
EventLog.cs

Mencari semua log peristiwa di komputer lokal dan membuat array EventLog objek yang berisi daftar.

public:
 static cli::array <System::Diagnostics::EventLog ^> ^ GetEventLogs();
public static System.Diagnostics.EventLog[] GetEventLogs ();
static member GetEventLogs : unit -> System.Diagnostics.EventLog[]
Public Shared Function GetEventLogs () As EventLog()

Mengembalikan

Array jenis EventLog yang mewakili log di komputer lokal.

Pengecualian

Anda tidak memiliki akses baca ke registri.

-atau-

Tidak ada layanan log peristiwa pada komputer.

Contoh

Contoh berikut menghitung log peristiwa yang ditentukan di komputer lokal, dan menampilkan detail konfigurasi untuk setiap log peristiwa.

void DisplayEventLogProperties()
{
   
   // Iterate through the current set of event log files,
   // displaying the property settings for each file.
   array<EventLog^>^eventLogs = EventLog::GetEventLogs();
   System::Collections::IEnumerator^ myEnum = eventLogs->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      EventLog^ e = safe_cast<EventLog^>(myEnum->Current);
      Int64 sizeKB = 0;
      Console::WriteLine();
      Console::WriteLine( "{0}:", e->LogDisplayName );
      Console::WriteLine( "  Log name = \t\t {0}", e->Log );
      Console::WriteLine( "  Number of event log entries = {0}", e->Entries->Count );
      
      // Determine if there is a file for this event log.
      RegistryKey ^ regEventLog = Registry::LocalMachine->OpenSubKey( String::Format( "System\\CurrentControlSet\\Services\\EventLog\\{0}", e->Log ) );
      if ( regEventLog )
      {
         Object^ temp = regEventLog->GetValue( "File" );
         if ( temp != nullptr )
         {
            Console::WriteLine( "  Log file path = \t {0}", temp );
            FileInfo^ file = gcnew FileInfo( temp->ToString() );
            
            // Get the current size of the event log file.
            if ( file->Exists )
            {
               sizeKB = file->Length / 1024;
               if ( (file->Length % 1024) != 0 )
               {
                  sizeKB++;
               }
               Console::WriteLine( "  Current size = \t {0} kilobytes", sizeKB );
            }
         }
         else
         {
            Console::WriteLine( "  Log file path = \t <not set>" );
         }
      }
      
      // Display the maximum size and overflow settings.
      sizeKB = e->MaximumKilobytes;
      Console::WriteLine( "  Maximum size = \t {0} kilobytes", sizeKB );
      Console::WriteLine( "  Overflow setting = \t {0}", e->OverflowAction );
      switch ( e->OverflowAction )
      {
         case OverflowAction::OverwriteOlder:
            Console::WriteLine( "\t Entries are retained a minimum of {0} days.", e->MinimumRetentionDays );
            break;

         case OverflowAction::DoNotOverwrite:
            Console::WriteLine( "\t Older entries are not overwritten." );
            break;

         case OverflowAction::OverwriteAsNeeded:
            Console::WriteLine( "\t If number of entries equals max size limit, a new event log entry overwrites the oldest entry." );
            break;

         default:
            break;
      }
   }
}
static void DisplayEventLogProperties()
{
    // Iterate through the current set of event log files,
    // displaying the property settings for each file.

    EventLog[] eventLogs = EventLog.GetEventLogs();
    foreach (EventLog e in eventLogs)
    {
        Int64 sizeKB = 0;

        Console.WriteLine();
        Console.WriteLine("{0}:", e.LogDisplayName);
        Console.WriteLine("  Log name = \t\t {0}", e.Log);

        Console.WriteLine("  Number of event log entries = {0}", e.Entries.Count.ToString());

        // Determine if there is an event log file for this event log.
        RegistryKey regEventLog = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\EventLog\\" + e.Log);
        if (regEventLog != null)
        {
            Object temp = regEventLog.GetValue("File");
            if (temp != null)
            {
                Console.WriteLine("  Log file path = \t {0}", temp.ToString());
                FileInfo file = new FileInfo(temp.ToString());

                // Get the current size of the event log file.
                if (file.Exists)
                {
                    sizeKB = file.Length / 1024;
                    if ((file.Length % 1024) != 0)
                    {
                        sizeKB++;
                    }
                    Console.WriteLine("  Current size = \t {0} kilobytes", sizeKB.ToString());
                }
            }
            else
            {
                Console.WriteLine("  Log file path = \t <not set>");
            }
        }

        // Display the maximum size and overflow settings.

        sizeKB = e.MaximumKilobytes;
        Console.WriteLine("  Maximum size = \t {0} kilobytes", sizeKB.ToString());
        Console.WriteLine("  Overflow setting = \t {0}", e.OverflowAction.ToString());

        switch (e.OverflowAction)
        {
            case OverflowAction.OverwriteOlder:
                Console.WriteLine("\t Entries are retained a minimum of {0} days.",
                    e.MinimumRetentionDays);
                break;
            case OverflowAction.DoNotOverwrite:
                Console.WriteLine("\t Older entries are not overwritten.");
                break;
            case OverflowAction.OverwriteAsNeeded:
                Console.WriteLine("\t If number of entries equals max size limit, a new event log entry overwrites the oldest entry.");
                break;
            default:
                break;
        }
    }
}
Shared Sub DisplayEventLogProperties()

   ' Iterate through the current set of event log files,
   ' displaying the property settings for each file.
   Dim eventLogs As EventLog() = EventLog.GetEventLogs()
   
   Dim e As EventLog
   For Each e In  eventLogs
      Dim sizeKB As Int64 = 0
      
      Console.WriteLine()
      Console.WriteLine("{0}:", e.LogDisplayName)
      Console.WriteLine("  Log name = " + ControlChars.Tab _
                          + ControlChars.Tab + " {0}", e.Log)

      Console.WriteLine("  Number of event log entries = {0}", e.Entries.Count.ToString())
      
      ' Determine if there is an event log file for this event log.
      Dim regEventLog As RegistryKey
      regEventLog = Registry.LocalMachine.OpenSubKey( _
             ("System\CurrentControlSet\Services\EventLog\" + e.Log))

      If Not (regEventLog Is Nothing) Then

         Dim temp As Object = regEventLog.GetValue("File")
         If Not (temp Is Nothing) Then

            Console.WriteLine("  Log file path = " + ControlChars.Tab _
                                  + " {0}", temp.ToString())
            Dim file As New FileInfo(temp.ToString())
            
            ' Get the current size of the event log file.
            If file.Exists Then
               sizeKB = file.Length / 1024
               If file.Length Mod 1024 <> 0 Then
                  sizeKB += 1
               End If
               Console.WriteLine("  Current size = " + ControlChars.Tab _
                          + " {0} kilobytes", sizeKB.ToString())
            End If
         Else
            Console.WriteLine("  Log file path = " + ControlChars.Tab _
                             + " <not set>")
         End If
      End If
      
      ' Display the maximum size and overflow settings.
      sizeKB = e.MaximumKilobytes
      Console.WriteLine("  Maximum size = " + ControlChars.Tab _
                         + " {0} kilobytes", sizeKB.ToString())
      Console.WriteLine("  Overflow setting = " + ControlChars.Tab _
                         + " {0}", e.OverflowAction.ToString())
      
      Select Case e.OverflowAction
         Case OverflowAction.OverwriteOlder
            Console.WriteLine(ControlChars.Tab + _
                 " Entries are retained a minimum of {0} days.", _
                 e.MinimumRetentionDays)
         Case OverflowAction.DoNotOverwrite
            Console.WriteLine(ControlChars.Tab + _
                 " Older entries are not overwritten.")
         Case OverflowAction.OverwriteAsNeeded
            Console.WriteLine(ControlChars.Tab + _
                 " If number of entries equals max size limit, a new event log entry overwrites the oldest entry.")
         Case Else
      End Select

   Next e

End Sub

Keterangan

Array EventLog objek adalah rekam jepret dari semua log peristiwa di komputer lokal saat panggilan ke GetEventLogs dilakukan. Ini bukan koleksi dinamis, sehingga tidak mencerminkan penghapusan atau pembuatan log secara real time. Anda harus memverifikasi bahwa log dalam array ada sebelum Anda membaca atau menulisnya. Array biasanya mencakup setidaknya tiga log: Aplikasi, Sistem, dan Keamanan. Jika Anda membuat log kustom di komputer lokal, log tersebut juga akan muncul di array.

Untuk mengambil daftar log peristiwa, Anda harus memiliki izin registri yang sesuai. Izin ini identik dengan yang diperlukan untuk memanggil Exists dan SourceExists.

Lihat juga

Berlaku untuk