EventInstance 類別

定義

表示事件記錄檔項目的語言中立資訊。

public ref class EventInstance
public class EventInstance
type EventInstance = class
Public Class EventInstance
繼承
EventInstance

範例

下列程式代碼範例會寫入資訊事件專案,然後重複使用 EventInstance ,將警告事件的專案寫入現有的事件記錄檔。 事件消息正文是在訊息資源檔中使用資源標識碼來指定。 程式代碼範例假設已為來源註冊對應的訊息資源檔。

// Ensure that the source has already been registered using
// EventLogInstaller or EventLog.CreateEventSource.
String^ sourceName = "SampleApplicationSource";
if ( EventLog::SourceExists( sourceName ) )
{
   // Define an informational event with no category.
   // The message identifier corresponds to the message text in the
   // message resource file defined for the source.
   EventInstance ^ myEvent = gcnew EventInstance( UpdateCycleCompleteMsgId,0 );

   // Write the event to the event log using the registered source.
   EventLog::WriteEvent( sourceName, myEvent, 0 );

   // Reuse the event data instance for another event entry.
   // Set the entry category and message identifiers for
   // the appropriate resource identifiers in the resource files
   // for the registered source.  Set the event type to Warning.
   myEvent->CategoryId = RefreshCategoryMsgId;
   myEvent->EntryType = EventLogEntryType::Warning;
   myEvent->InstanceId = ServerConnectionDownMsgId;

   // Write the event to the event log using the registered source.
   // Insert the machine name into the event message text.
   array<String^>^ss = {Environment::MachineName};
   EventLog::WriteEvent( sourceName, myEvent, ss );
}
else
{
   Console::WriteLine( "Warning - event source {0} not registered", sourceName );
}

// Ensure that the source has already been registered using
// EventLogInstaller or EventLog.CreateEventSource.

string sourceName = "SampleApplicationSource";
if(EventLog.SourceExists(sourceName))
{
    // Define an informational event with no category.
    // The message identifier corresponds to the message text in the
    // message resource file defined for the source.
    EventInstance myEvent = new EventInstance(UpdateCycleCompleteMsgId, 0);

    // Write the event to the event log using the registered source.
    EventLog.WriteEvent(sourceName, myEvent);

    // Reuse the event data instance for another event entry.
    // Set the entry category and message identifiers for
    // the appropriate resource identifiers in the resource files
    // for the registered source.  Set the event type to Warning.

    myEvent.CategoryId = RefreshCategoryMsgId;
    myEvent.EntryType = EventLogEntryType.Warning;
    myEvent.InstanceId = ServerConnectionDownMsgId;

    // Write the event to the event log using the registered source.
    // Insert the machine name into the event message text.
    EventLog.WriteEvent(sourceName, myEvent, Environment.MachineName);
}
else
{
    Console.WriteLine("Warning - event source {0} not registered",
        sourceName);
}
' Ensure that the source has already been registered using
' EventLogInstaller or EventLog.CreateEventSource.
Dim sourceName as String = "SampleApplicationSource"
If EventLog.SourceExists(sourceName)
    
    ' Define an informational event with no category.
    ' The message identifier corresponds to the message text in the
    ' message resource file defined for the source.
    Dim myEvent As EventInstance = New EventInstance(UpdateCycleCompleteMsgId, 0)
    ' Write the event to the event log using the registered source.
    EventLog.WriteEvent(sourceName, myEvent)

    ' Reuse the event data instance for another event entry.
    ' Set the entry category and message identifiers for
    ' the appropriate resource identifiers in the resource files
    ' for the registered source.  Set the event type to Warning.

    myEvent.CategoryId = RefreshCategoryMsgId
    myEvent.EntryType = EventLogEntryType.Warning
    myEvent.InstanceId = ServerConnectionDownMsgId

    ' Write the event to the event log using the registered source.
    ' Insert the machine name into the event message text.
    EventLog.WriteEvent(sourceName, myEvent, Environment.MachineName)

Else 
    Console.WriteLine("Warning - event source {0} not registered", _
        sourceName)
End If

程式代碼範例會使用下列訊息文本檔,內建於資源庫 EventLogMsgs.dll。 訊息文本檔是建立訊息資源檔的來源。 訊息文字檔會定義類別、事件訊息和參數插入字串的資源識別碼和文字。

; // EventLogMsgs.mc  
; // ********************************************************  

; // Use the following commands to build this file:  

; //   mc -s EventLogMsgs.mc  
; //   rc EventLogMsgs.rc  
; //   link /DLL /SUBSYSTEM:WINDOWS /NOENTRY /MACHINE:x86 EventLogMsgs.Res   
; // ********************************************************  

; // - Event categories -  
; // Categories must be numbered consecutively starting at 1.  
; // ********************************************************  

MessageId=0x1  
Severity=Success  
SymbolicName=INSTALL_CATEGORY  
Language=English  
Installation  
.  

MessageId=0x2  
Severity=Success  
SymbolicName=QUERY_CATEGORY  
Language=English  
Database Query  
.  

MessageId=0x3  
Severity=Success  
SymbolicName=REFRESH_CATEGORY  
Language=English  
Data Refresh  
.  

; // - Event messages -  
; // *********************************  

MessageId = 1000  
Severity = Success  
Facility = Application  
SymbolicName = AUDIT_SUCCESS_MESSAGE_ID_1000  
Language=English  
My application message text, in English, for message id 1000, called from %1.  
.  

MessageId = 1001  
Severity = Warning  
Facility = Application  
SymbolicName = AUDIT_FAILED_MESSAGE_ID_1001  
Language=English  
My application message text, in English, for message id 1001, called from %1.  
.  

MessageId = 1002  
Severity = Success  
Facility = Application  
SymbolicName = GENERIC_INFO_MESSAGE_ID_1002  
Language=English  
My generic information message in English, for message id 1002.  
.  

MessageId = 1003  
Severity = Warning  
Facility = Application  
SymbolicName = GENERIC_WARNING_MESSAGE_ID_1003  
Language=English  
My generic warning message in English, for message id 1003, called from %1.  
.  

MessageId = 1004  
Severity = Success  
Facility = Application  
SymbolicName = UPDATE_CYCLE_COMPLETE_MESSAGE_ID_1004  
Language=English  
The update cycle is complete for %%5002.  
.  

MessageId = 1005  
Severity = Warning  
Facility = Application  
SymbolicName = SERVER_CONNECTION_DOWN_MESSAGE_ID_1005  
Language=English  
The refresh operation did not complete because the connection to server %1 could not be established.  
.  

; // - Event log display name -  
; // ********************************************************  

MessageId = 5001  
Severity = Success  
Facility = Application  
SymbolicName = EVENT_LOG_DISPLAY_NAME_MSGID  
Language=English  
Sample Event Log  
.  

; // - Event message parameters -  
; //   Language independent insertion strings  
; // ********************************************************  

MessageId = 5002  
Severity = Success  
Facility = Application  
SymbolicName = EVENT_LOG_SERVICE_NAME_MSGID  
Language=English  
SVC_UPDATE.EXE  
.  

備註

使用 EventInstance 來寫入具有資源標識符的事件記錄檔專案,而不是字串值。 若要寫入事件記錄檔專案,請初始化 屬性, InstanceId 並將 實例傳遞至 WriteEvent 方法。 事件檢視器 會使用實例標識符,根據目前的語言設定,從本地化的資源文件尋找並顯示對應的字串。 您必須先向對應的資源檔註冊事件來源,才能使用資源標識符來寫入事件。

撰寫事件時,您可以設定 EntryType 屬性來指定專案 事件檢視器 顯示的圖示。 您也可以指定 屬性,以指定 CategoryId 事件檢視器 針對項目顯示的類別。

事件檢視器 會使用 類別來篩選事件來源所寫入的事件。 事件檢視器 可以將類別顯示為數值,也可以使用類別做為資源標識符來顯示本地化的類別字串。

若要在 事件檢視器 中顯示本地化的類別字串,您必須使用以類別資源檔案設定的事件來源,並將 設定CategoryId為類別資源檔中的資源識別符。 如果事件來源沒有設定的類別資源檔,或指定的 CategoryId 不會為類別資源檔中的字元串編製索引,則 事件檢視器 會顯示該專案的數值類別值。 使用 EventLogInstallerEventSourceCreationData 類別,設定類別資源檔以及資源檔中的類別字串數目。

您必須先註冊事件記錄檔的來源,才能使用 EventInstance 方法寫入事件 WriteEvent 。 來源必須設定為將當地語系化專案寫入記錄檔,而且來源必須至少定義訊息資源檔。

在安裝應用程式期間 Create 新的事件來源。 這可讓操作系統重新整理其已註冊事件來源及其設定的清單。 如果操作系統尚未重新整理其事件來源清單,而且您嘗試使用新的來源寫入事件,寫入作業將會失敗。 您可以使用 或 CreateEventSource 方法來設定新的來源EventLogInstaller。 您必須擁有計算機上的系統管理許可權,才能建立新的事件來源。

如需定義事件訊息和建置事件記錄檔資源文件的詳細資訊,請參閱 Platform SDK 檔中的 訊息編譯 程式一文。

建構函式

EventInstance(Int64, Int32)

使用事件項目的當地語系化訊息和分類文字的指定資源識別項,初始化 EventInstance 類別的新執行個體。

EventInstance(Int64, Int32, EventLogEntryType)

使用事件項目的當地語系化訊息和分類文字的指定資源識別項,以及指定的事件記錄檔項目型別,初始化 EventInstance 類別的新執行個體。

屬性

CategoryId

取得或設定資源識別項,指定應用程式定義的事件項目分類。

EntryType

取得或設定事件記錄檔項目的事件型別。

InstanceId

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

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱