EventLogEntry 類別

定義

將單一記錄封裝於事件記錄檔中。 此類別無法獲得繼承。

public ref class EventLogEntry sealed : System::ComponentModel::Component, System::Runtime::Serialization::ISerializable
public sealed class EventLogEntry : System.ComponentModel.Component, System.Runtime.Serialization.ISerializable
[System.Serializable]
public sealed class EventLogEntry : System.ComponentModel.Component, System.Runtime.Serialization.ISerializable
type EventLogEntry = class
    inherit Component
    interface ISerializable
[<System.Serializable>]
type EventLogEntry = class
    inherit Component
    interface ISerializable
Public NotInheritable Class EventLogEntry
Inherits Component
Implements ISerializable
繼承
屬性
實作

範例

下列程式代碼範例示範 類別的使用 EventLogEntry 。 在此範例中, switch 語句會使用主控台輸入來搜尋指定事件類型的事件記錄專案。 如果找到相符專案,記錄專案來源信息會顯示在主控台。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;

int main()
{
   String^ myEventType = nullptr;
   
   // Associate the instance of 'EventLog' with local System Log.
   EventLog^ myEventLog = gcnew EventLog( "System","." );
   Console::WriteLine( "1:Error" );
   Console::WriteLine( "2:Information" );
   Console::WriteLine( "3:Warning" );
   Console::WriteLine( "Select the Event Type" );
   int myOption = Convert::ToInt32( Console::ReadLine() );
   switch ( myOption )
   {
      case 1:
         myEventType = "Error";
         break;

      case 2:
         myEventType = "Information";
         break;

      case 3:
         myEventType = "Warning";
         break;

      default:
         break;
   }
   EventLogEntryCollection^ myLogEntryCollection = myEventLog->Entries;
   int myCount = myLogEntryCollection->Count;
   
   // Iterate through all 'EventLogEntry' instances in 'EventLog'.
   for ( int i = myCount - 1; i > -1; i-- )
   {
      EventLogEntry^ myLogEntry = myLogEntryCollection[ i ];
      
      // Select the entry having desired EventType.
      if ( myLogEntry->EntryType.Equals( myEventType ) )
      {
         // Display Source of the event.
         Console::WriteLine( "{0} was the source of last event of type {1}", myLogEntry->Source, myLogEntry->EntryType );
         return 0;
      }
   }
}
using System;
using System.Diagnostics;
   class MyEventlogClass
   {
      public static void Main()
      {
         String myEventType=null;
         // Associate the instance of 'EventLog' with local System Log.
         EventLog myEventLog = new EventLog("System", ".");
         Console.WriteLine("1:Error");
         Console.WriteLine("2:Information");
         Console.WriteLine("3:Warning");
         Console.WriteLine("Select the Event Type");
         int myOption=Convert.ToInt32(Console.ReadLine());
         switch(myOption)
         {
            case 1:  myEventType="Error";
                     break;
            case 2:  myEventType="Information";
                     break;
            case 3:  myEventType="Warning";
                     break;
            default: break;
         }

            EventLogEntryCollection myLogEntryCollection=myEventLog.Entries;
            int myCount =myLogEntryCollection.Count;
            // Iterate through all 'EventLogEntry' instances in 'EventLog'.
            for(int i=myCount-1;i>-1;i--)
            {
               EventLogEntry myLogEntry = myLogEntryCollection[i];
               // Select the entry having desired EventType.
               if(myLogEntry.EntryType.ToString().Equals(myEventType))
               {
                  // Display Source of the event.
                  Console.WriteLine(myLogEntry.Source
                     +" was the source of last event of type "
                     +myLogEntry.EntryType);
                  return;
               }
            }
         }
   }
Imports System.Diagnostics

Class MyEventlogClass
   Public Shared Sub Main()
      Dim myEventType As String = Nothing
      ' Associate the instance of 'EventLog' with local System Log.
      Dim myEventLog As New EventLog("System", ".")
      Console.WriteLine("1:Error")
      Console.WriteLine("2:Information")
      Console.WriteLine("3:Warning")
      Console.WriteLine("Select the Event Type")
      Dim myOption As Integer = Convert.ToInt32(Console.ReadLine())
      Select Case myOption
         Case 1
            myEventType = "Error"
         Case 2
            myEventType = "Information"
         Case 3
            myEventType = "Warning"
         Case Else
      End Select

      Dim myLogEntryCollection As EventLogEntryCollection = myEventLog.Entries
      Dim myCount As Integer = myLogEntryCollection.Count
      ' Iterate through all 'EventLogEntry' instances in 'EventLog'.
      Dim i As Integer
      For i = myCount - 1 To 0 Step -1
         Dim myLogEntry As EventLogEntry = myLogEntryCollection(i)
         ' Select the entry having desired EventType.
         If myLogEntry.EntryType.ToString().Equals(myEventType) Then
            ' Display Source of the event.
            Console.WriteLine(myLogEntry.Source + " was the source of last "& _
                             "event of type " & myLogEntry.EntryType.ToString())
            Return
         End If
      Next 
   End Sub
End Class

備註

使用 類別時,您通常不會直接建立 的EventLogEntryEventLog實例。 類別 Entries 的成員 EventLog 包含實例的 EventLogEntry 集合,您可以在使用 EventLogEntryCollection.Item[] 類別索引成員讀取時逐一查看。

重要

此型別代表 IDisposable 介面。 當您完成使用型別時,您應該直接或間接處置它。 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。 若要間接處置它,請使用語言建構函式,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Category

取得與這個項目的 CategoryNumber 屬性相關聯的文字。

CategoryNumber

取得事件記錄檔項目的分類編號。

Container

取得包含 IContainerComponent

(繼承來源 Component)
Data

取得與項目相關聯的二進位資料。

DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
EntryType

取得這個項目的事件類型。

EventID
已淘汰.
已淘汰.
已淘汰.

取得目前事件項目的應用程式特定事件識別項。

Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
Index

取得事件記錄檔中這個項目的索引。

InstanceId

取得資源識別項,指定事件項目的訊息文字。

MachineName

取得產生這個項目的電腦名稱。

Message

取得與這個事件項目相關聯的當地語系化訊息。

ReplacementStrings

取得與事件記錄檔項目相關聯的取代字串。

Site

取得或設定 ComponentISite

(繼承來源 Component)
Source

取得產生這個事件的應用程式名稱。

TimeGenerated

取得產生這個事件的當地時間。

TimeWritten

取得將這個事件寫入至記錄檔的當地時間。

UserName

取得為這個事件負責的使用者名稱。

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Equals(EventLogEntry)

在兩個事件記錄檔項目之間進行比較。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)

事件

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將序列化目標物件所需的資料填入 SerializationInfo

適用於

另請參閱