EventLog.WriteEvent 方法

定義

將當地語系化的事件項目寫入事件記錄檔。

多載

WriteEvent(String, EventInstance, Object[])

以指定的事件資料、訊息取代字串,以及使用指定的已登錄事件來源,寫入事件記錄檔項目。

WriteEvent(EventInstance, Object[])

將當地語系化的項目寫入事件記錄檔。

WriteEvent(EventInstance, Byte[], Object[])

以指定的事件資料、訊息取代字串和相關聯的二進位資料來寫入事件記錄檔項目。

WriteEvent(String, EventInstance, Byte[], Object[])

以指定的事件資料、訊息取代字串和相關聯的二進位資料,以及使用指定的已登錄事件來源,寫入事件記錄檔項目。

WriteEvent(String, EventInstance, Object[])

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

以指定的事件資料、訊息取代字串,以及使用指定的已登錄事件來源,寫入事件記錄檔項目。

C#
public static void WriteEvent (string source, System.Diagnostics.EventInstance instance, params object[] values);

參數

source
String

為指定電腦上的應用程式註冊的事件來源名稱。

instance
EventInstance

EventInstance 執行個體,表示當地語系化的事件記錄項目。

values
Object[]

要合併到事件記錄項目訊息文字的字串陣列。

例外狀況

source 值是空白字串 ("")。

-或-

source 值為 null

-或-

instance.InstanceId 小於零或大於 UInt16.MaxValue

-或-

values 擁有超過 256 個項目。

-或-

其中一個 values 項目的長度超過 32766 個位元組。

-或-

來源名稱產生的登錄機碼路徑長度超過 254 個字元。

instancenull

無法開啟事件記錄檔的登錄機碼。

作業系統在將事件項目寫入事件記錄檔時回報錯誤。 無法使用 Windows 錯誤碼。

範例

下列範例會將參考事件專案和警告事件專案寫入現有的事件記錄檔。 事件消息正文是在資源檔中使用資源標識碼來指定。 此範例假設對應的資源檔已註冊來源。

C#

string sourceName = "SampleApplicationSource";
if(EventLog.SourceExists(sourceName))
{

    // Define an informational event and a warning event.

    // The message identifiers correspond to the message text in the
    // message resource file defined for the source.
    EventInstance myInfoEvent = new EventInstance(InformationMsgId, 0, EventLogEntryType.Information);
    EventInstance myWarningEvent = new EventInstance(WarningMsgId, 0, EventLogEntryType.Warning);

    // Insert the method name into the event log message.
    string [] insertStrings = {"EventLogSamples.WriteEventSample2"};

    // Write the events to the event log.

    EventLog.WriteEvent(sourceName, myInfoEvent);

    // Append binary data to the warning event entry.
    byte [] binaryData = { 7, 8, 9, 10 };
    EventLog.WriteEvent(sourceName, myWarningEvent, binaryData, insertStrings);
}
else
{
    Console.WriteLine("Warning - event source {0} not registered",
        sourceName);
}

此範例會使用下列訊息文本檔,內建於資源庫 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  
.  

備註

使用這個方法可將當地語系化專案寫入事件記錄檔,並使用已註冊為適當記錄的事件來源。 您可以使用資源識別碼來指定事件屬性,而不是字串值。 事件檢視器 會使用資源識別碼,顯示來源本地化資源檔中的對應字串。 您必須先向對應的資源檔註冊來源,才能使用資源標識碼寫入事件。

輸入 instance 實例會指定事件訊息和屬性。 InstanceId instance設定來源訊息資源檔案中已定義訊息之輸入的 。 您可以選擇性地設定 CategoryId 輸入的 和 EntryTypeinstance ,以定義事件項目的類別和事件類型。 您也可以指定與語言無關的字串數位,以插入當地語系化訊息正文中。 null如果事件訊息不包含取代字串的格式佔位元元,請將 設定values為 。

使用 之前 WriteEvent,必須先為事件記錄檔註冊指定的來源。 指定的來源必須設定為將本地化專案寫入記錄檔;來源至少必須定義訊息資源檔。

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

來源必須設定為撰寫本地化專案,或撰寫直接字串。 WriteEntry如果您的應用程式將字串值直接寫入事件記錄檔,請使用方法。

如果您的應用程式同時使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。 例如,使用資源文件設定一個來源,然後在方法中使用 WriteEvent 該來源,使用資源標識符將專案寫入事件記錄檔。 然後建立不含資源檔的不同來源,並在方法中使用 WriteEntry 該來源直接將字串寫入事件記錄檔。

另請參閱

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

WriteEvent(EventInstance, Object[])

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

將當地語系化的項目寫入事件記錄檔。

C#
public void WriteEvent (System.Diagnostics.EventInstance instance, params object[] values);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public void WriteEvent (System.Diagnostics.EventInstance instance, params object[] values);

參數

instance
EventInstance

EventInstance 執行個體,表示當地語系化的事件記錄項目。

values
Object[]

要合併到事件記錄項目訊息文字的字串陣列。

屬性

例外狀況

還沒有設定 SourceEventLog 屬性。

-或-

該方法已嘗試登錄新的事件來源,但 MachineName 中的電腦名稱無效。

-或-

來源已經登錄用於不同的事件記錄檔。

-或-

instance.InstanceId 小於零或大於 UInt16.MaxValue

-或-

values 擁有超過 256 個項目。

-或-

其中一個 values 項目的長度超過 32766 個位元組。

-或-

來源名稱產生的登錄機碼路徑長度超過 254 個字元。

instancenull

無法開啟事件記錄檔的登錄機碼。

作業系統在將事件項目寫入事件記錄檔時回報錯誤。 無法使用 Windows 錯誤碼。

範例

下列範例會將兩個稽核專案寫入事件記錄檔 myNewLog中。 如果本機計算機上不存在新的事件來源,此範例會建立新的事件來源和新的事件記錄檔。 事件消息正文是在資源檔中使用資源標識碼來指定。

C#

// Create the event source if it does not exist.
string sourceName = "SampleApplicationSource";
if(!EventLog.SourceExists(sourceName))
{
    // Call a local method to register the event log source
    // for the event log "myNewLog."  Use the resource file
    // EventLogMsgs.dll in the current directory for message text.

    string messageFile =  String.Format("{0}\\{1}",
        System.Environment.CurrentDirectory,
        "EventLogMsgs.dll");

    CreateEventSourceSample1(messageFile);
}

// Get the event log corresponding to the existing source.
string myLogName = EventLog.LogNameFromSourceName(sourceName,".");

EventLog myEventLog = new EventLog(myLogName, ".", sourceName);

// Define two audit events.

// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance myAuditSuccessEvent = new EventInstance(AuditSuccessMsgId, 0, EventLogEntryType.SuccessAudit);
EventInstance myAuditFailEvent = new EventInstance(AuditFailedMsgId, 0, EventLogEntryType.FailureAudit);

// Insert the method name into the event log message.
string [] insertStrings = {"EventLogSamples.WriteEventSample1"};

// Write the events to the event log.

myEventLog.WriteEvent(myAuditSuccessEvent, insertStrings);

// Append binary data to the audit failure event entry.
byte [] binaryData = { 3, 4, 5, 6 };
myEventLog.WriteEvent(myAuditFailEvent, binaryData, insertStrings);

此範例會使用下列訊息文本檔,內建於資源庫 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  
.  

備註

使用這個方法,將本地化的專案寫入事件記錄檔。 您可以使用資源識別碼來指定事件屬性,而不是字串值。 事件檢視器 會使用資源識別碼,顯示 本地化資源檔中Source的對應字串。 您必須先向對應的資源檔註冊來源,才能使用資源標識碼寫入事件。

輸入 instance 實例會指定事件訊息和屬性。 InstanceId instance設定來源訊息資源檔案中已定義訊息之輸入的 。 您可以選擇性地設定 CategoryId 輸入的 和 EntryTypeinstance ,以定義事件項目的類別和事件類型。 您也可以指定與語言無關的字串數位,以插入當地語系化訊息正文中。 null如果事件訊息不包含取代字串的格式佔位元元,請將 設定values為 。

您必須先在元件上EventLog設定 屬性,Source才能使用 WriteEvent。 指定的來源必須設定為將本地化專案寫入記錄檔;來源至少必須定義訊息資源檔。

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

來源必須設定為撰寫本地化專案,或撰寫直接字串。 WriteEntry如果您的應用程式直接將字串值寫入事件記錄檔,請使用方法。

如果您的應用程式同時使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。 例如,使用資源文件設定一個來源,然後在方法中使用 WriteEvent 該來源,使用資源標識符將專案寫入事件記錄檔。 然後,在沒有資源文件的情況下建立不同的來源,然後在方法中使用 WriteEntry 該來源直接將字元串寫入事件記錄檔。

注意

如果您將專案寫入遠端電腦,如果遠端電腦未執行 .NET Framework,字串的值message可能不是您預期的值。 此外, message 字串不能包含 %n,其中 n 是整數值 (,例如 %1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

另請參閱

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

WriteEvent(EventInstance, Byte[], Object[])

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

以指定的事件資料、訊息取代字串和相關聯的二進位資料來寫入事件記錄檔項目。

C#
public void WriteEvent (System.Diagnostics.EventInstance instance, byte[] data, params object[] values);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public void WriteEvent (System.Diagnostics.EventInstance instance, byte[] data, params object[] values);

參數

instance
EventInstance

EventInstance 執行個體,表示當地語系化的事件記錄項目。

data
Byte[]

保存與項目相關的二進位資料的位元組陣列。

values
Object[]

要合併到事件記錄項目訊息文字的字串陣列。

屬性

例外狀況

還沒有設定 SourceEventLog 屬性。

-或-

該方法已嘗試登錄新的事件來源,但 MachineName 中的電腦名稱無效。

-或-

來源已經登錄用於不同的事件記錄檔。

-或-

instance.InstanceId 小於零或大於 UInt16.MaxValue

-或-

values 擁有超過 256 個項目。

-或-

其中一個 values 項目的長度超過 32766 個位元組。

-或-

來源名稱產生的登錄機碼路徑長度超過 254 個字元。

instancenull

無法開啟事件記錄檔的登錄機碼。

作業系統在將事件項目寫入事件記錄檔時回報錯誤。 無法使用 Windows 錯誤碼。

範例

下列範例會將兩個稽核專案寫入事件記錄檔 myNewLog。 如果本機計算機上不存在新的事件來源,則此範例會建立新的事件來源和新的事件記錄檔。 事件消息正文是在資源檔中使用資源標識碼來指定。

C#

// Create the event source if it does not exist.
string sourceName = "SampleApplicationSource";
if(!EventLog.SourceExists(sourceName))
{
    // Call a local method to register the event log source
    // for the event log "myNewLog."  Use the resource file
    // EventLogMsgs.dll in the current directory for message text.

    string messageFile =  String.Format("{0}\\{1}",
        System.Environment.CurrentDirectory,
        "EventLogMsgs.dll");

    CreateEventSourceSample1(messageFile);
}

// Get the event log corresponding to the existing source.
string myLogName = EventLog.LogNameFromSourceName(sourceName,".");

EventLog myEventLog = new EventLog(myLogName, ".", sourceName);

// Define two audit events.

// The message identifiers correspond to the message text in the
// message resource file defined for the source.
EventInstance myAuditSuccessEvent = new EventInstance(AuditSuccessMsgId, 0, EventLogEntryType.SuccessAudit);
EventInstance myAuditFailEvent = new EventInstance(AuditFailedMsgId, 0, EventLogEntryType.FailureAudit);

// Insert the method name into the event log message.
string [] insertStrings = {"EventLogSamples.WriteEventSample1"};

// Write the events to the event log.

myEventLog.WriteEvent(myAuditSuccessEvent, insertStrings);

// Append binary data to the audit failure event entry.
byte [] binaryData = { 3, 4, 5, 6 };
myEventLog.WriteEvent(myAuditFailEvent, binaryData, insertStrings);

此範例會使用下列訊息文本檔,內建於資源庫 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  
.  

備註

使用這個方法,將具有其他事件特定數據的當地語系化專案寫入事件記錄檔。 您可以使用資源識別碼來指定事件屬性,而不是字串值。 事件檢視器 會使用資源識別符,從的當地語系化資源文件Source顯示對應的字串。 您必須先向對應的資源檔註冊來源,才能使用資源標識碼寫入事件。

輸入 instance 實例會指定事件訊息和屬性。 InstanceId設定來源訊息資源檔案中已定義訊息之輸入的 instance 。 您可以選擇性地設定 CategoryIdEntryTypeinstance 輸入,以定義事件項目的類別和事件類型。 您也可以指定與語言無關的字串數位,以插入當地語系化訊息正文中。 null如果事件訊息不包含取代字串的格式佔位元元,請將 設定values為 。

當需要提供事件的其他詳細數據時,請指定具有事件的二進位數據。 例如,使用 data 參數來包含特定錯誤的相關信息。 事件檢視器 不會解譯相關聯的事件數據;它會以合併的十六進位和文字格式顯示數據。 謹慎使用事件特定的數據;只有在確定它很有用時,才包含它。 您也可以使用事件特定資料來儲存應用程式可以獨立於 事件檢視器 處理的資訊。 例如,您可以特別為您的事件撰寫查看器,或撰寫程式來掃描事件記錄檔,並建立包含事件特定數據資訊的報表。

您必須先在元件上EventLog設定 Source 屬性,才能使用 WriteEvent。 指定的來源必須設定為將本地化專案寫入記錄檔;來源至少必須定義訊息資源檔。

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

注意

如果您在呼叫 WriteEvent之前未為 EventLog 實例指定 MachineName ,本機電腦 (“。假設 ) 。

來源必須設定為撰寫本地化專案,或撰寫直接字串。 WriteEntry如果您的應用程式直接將字串值寫入事件記錄檔,請使用方法。

如果您的應用程式同時使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。 例如,使用資源文件設定一個來源,然後在方法中使用 WriteEvent 該來源,使用資源標識符將專案寫入事件記錄檔。 然後,在沒有資源文件的情況下建立不同的來源,然後在方法中使用 WriteEntry 該來源直接將字元串寫入事件記錄檔。

注意

如果您將專案寫入遠端電腦,如果遠端電腦未執行 .NET Framework,字串的值message可能不是您預期的值。 此外, message 字串不能包含 %n,其中 n 是整數值 (,例如 %1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

另請參閱

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

WriteEvent(String, EventInstance, Byte[], Object[])

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

以指定的事件資料、訊息取代字串和相關聯的二進位資料,以及使用指定的已登錄事件來源,寫入事件記錄檔項目。

C#
public static void WriteEvent (string source, System.Diagnostics.EventInstance instance, byte[] data, params object[] values);

參數

source
String

為指定電腦上的應用程式註冊的事件來源名稱。

instance
EventInstance

EventInstance 執行個體,表示當地語系化的事件記錄項目。

data
Byte[]

保存與項目相關的二進位資料的位元組陣列。

values
Object[]

要合併到事件記錄項目訊息文字的字串陣列。

例外狀況

source 值是空白字串 ("")。

-或-

source 值為 null

-或-

instance.InstanceId 小於零或大於 UInt16.MaxValue

-或-

values 擁有超過 256 個項目。

-或-

其中一個 values 項目的長度超過 32766 個位元組。

-或-

來源名稱產生的登錄機碼路徑長度超過 254 個字元。

instancenull

無法開啟事件記錄檔的登錄機碼。

作業系統在將事件項目寫入事件記錄檔時回報錯誤。 無法使用 Windows 錯誤碼。

範例

下列範例會將參考事件專案和警告事件專案寫入現有的事件記錄檔。 事件消息正文是在資源檔中使用資源標識碼來指定。 此範例假設已向來源註冊對應的資源檔。

C#

string sourceName = "SampleApplicationSource";
if(EventLog.SourceExists(sourceName))
{

    // Define an informational event and a warning event.

    // The message identifiers correspond to the message text in the
    // message resource file defined for the source.
    EventInstance myInfoEvent = new EventInstance(InformationMsgId, 0, EventLogEntryType.Information);
    EventInstance myWarningEvent = new EventInstance(WarningMsgId, 0, EventLogEntryType.Warning);

    // Insert the method name into the event log message.
    string [] insertStrings = {"EventLogSamples.WriteEventSample2"};

    // Write the events to the event log.

    EventLog.WriteEvent(sourceName, myInfoEvent);

    // Append binary data to the warning event entry.
    byte [] binaryData = { 7, 8, 9, 10 };
    EventLog.WriteEvent(sourceName, myWarningEvent, binaryData, insertStrings);
}
else
{
    Console.WriteLine("Warning - event source {0} not registered",
        sourceName);
}

此範例會使用下列訊息文本檔,內建於資源庫 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  
.  

備註

使用這個方法,將具有其他事件特定數據的當地語系化專案寫入事件記錄檔,並使用已經註冊為適當記錄的事件來源。 您可以使用資源識別碼來指定事件屬性,而不是字串值。 事件檢視器 會使用資源識別碼,從來源的當地語系化資源文件顯示對應的字串。 您必須先向對應的資源檔註冊來源,才能使用資源標識碼寫入事件。

輸入 instance 實例會指定事件訊息和屬性。 InstanceId instance設定來源訊息資源檔案中已定義訊息之輸入的 。 您可以選擇性地設定 CategoryId 輸入的 和 EntryTypeinstance ,以定義事件項目的類別和事件類型。 您也可以指定與語言無關的字串數位,以插入當地語系化訊息正文中。 null如果事件訊息不包含取代字串的格式佔位元元,請將 設定values為 。

當需要提供事件的其他詳細數據時,請使用 事件指定二進位數據。 例如,使用 data 參數來包含特定錯誤的相關信息。 事件檢視器 不會解譯相關聯的事件數據;它會以合併的十六進位和文字格式顯示數據。 謹慎使用事件特定數據;只有在您確定它很有用時,才包含它。 您也可以使用事件特定資料來儲存應用程式可以獨立於 事件檢視器 處理的資訊。 例如,您可以特別撰寫事件的查看器,或撰寫程式來掃描事件記錄檔,並建立包含事件特定數據資訊的報表。

使用 之前 WriteEvent,必須先為事件記錄檔註冊指定的來源。 指定的來源必須設定為將本地化專案寫入記錄檔;來源至少必須定義訊息資源檔。

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

來源必須設定為撰寫本地化專案,或撰寫直接字串。 WriteEntry如果您的應用程式將字串值直接寫入事件記錄檔,請使用方法。

如果您的應用程式同時使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。 例如,使用資源文件設定一個來源,然後在方法中使用 WriteEvent 該來源,使用資源標識符將專案寫入事件記錄檔。 然後建立不含資源檔的不同來源,並在方法中使用 WriteEntry 該來源直接將字串寫入事件記錄檔。

另請參閱

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9