EventLog.WriteEvent EventLog.WriteEvent EventLog.WriteEvent EventLog.WriteEvent Method

定義

將當地語系化的事件項目寫入事件記錄檔。Writes a localized event entry to the event log.

多載

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

以指定的事件資料、訊息取代字串,以及使用指定的已登錄事件來源,寫入事件記錄檔項目。Writes an event log entry with the given event data and message replacement strings, using the specified registered event source.

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

將當地語系化的項目寫入事件記錄檔。Writes a localized entry to the event log.

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

以指定的事件資料、訊息取代字串和相關聯的二進位資料來寫入事件記錄檔項目。Writes an event log entry with the given event data, message replacement strings, and associated binary data.

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

以指定的事件資料、訊息取代字串和相關聯的二進位資料,以及使用指定的已登錄事件來源,寫入事件記錄檔項目。Writes an event log entry with the given event data, message replacement strings, and associated binary data, and using the specified registered event source.

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

以指定的事件資料、訊息取代字串,以及使用指定的已登錄事件來源,寫入事件記錄檔項目。Writes an event log entry with the given event data and message replacement strings, using the specified registered event source.

public:
 static void WriteEvent(System::String ^ source, System::Diagnostics::EventInstance ^ instance, ... cli::array <System::Object ^> ^ values);
public static void WriteEvent (string source, System.Diagnostics.EventInstance instance, params object[] values);
static member WriteEvent : string * System.Diagnostics.EventInstance * obj[] -> unit
Public Shared Sub WriteEvent (source As String, instance As EventInstance, ParamArray values As Object())

參數

source
String String String String

為指定電腦上的應用程式註冊的事件來源名稱。The name of the event source registered for the application on the specified computer.

instance
EventInstance EventInstance EventInstance EventInstance

EventInstance 執行個體,表示當地語系化的事件記錄項目。An EventInstance instance that represents a localized event log entry.

values
Object[]

要合併到事件記錄項目訊息文字的字串陣列。An array of strings to merge into the message text of the event log entry.

例外狀況

source 值是空白字串 ("")。The source value is an empty string ("").

-或--or- source 值為 nullThe source value is null.

-或--or- instance.InstanceId 小於零或大於 MaxValueinstance.InstanceId is less than zero or greater than MaxValue.

-或--or- values 擁有超過 256 個項目。values has more than 256 elements.

-或--or- 其中一個 values 項目的長度超過 32766 個位元組。One of the values elements is longer than 32766 bytes.

-或--or- 來源名稱產生的登錄機碼路徑長度超過 254 個字元。The source name results in a registry key path longer than 254 characters.

無法開啟事件記錄檔的登錄機碼。The registry key for the event log could not be opened.

作業系統在將事件項目寫入事件記錄檔時回報錯誤。The operating system reported an error when writing the event entry to the event log. 無法使用 Windows 錯誤碼。A Windows error code is not available.

範例

下列範例會將參考事件專案和警告事件專案寫入現有的事件記錄檔。The following example writes an informational event entry and a warning event entry to an existing event log. 事件郵件內文是使用資源檔中的資源識別碼所指定。The event message text is specified using a resource identifier in a resource file. 此範例假設對應的資源檔已針對來源註冊。The example assumes the corresponding resource file has been registered for the source.

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 = gcnew EventInstance( InformationMsgId,0,EventLogEntryType::Information );
   EventInstance ^ myWarningEvent = gcnew EventInstance( WarningMsgId,0,EventLogEntryType::Warning );
   
   // Insert the method name into the event log message.
   array<String^>^insertStrings = {"EventLogSamples.WriteEventSample2"};
   
   // Write the events to the event log.
   EventLog::WriteEvent( sourceName, myInfoEvent, 0 );
   
   // Append binary data to the warning event entry.
   array<Byte>^binaryData = {7,8,9,10};
   EventLog::WriteEvent( sourceName, myWarningEvent, binaryData, insertStrings );
}
else
{
   Console::WriteLine( "Warning - event source {0} not registered", sourceName );
}

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);
}
Dim sourceName As String = "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.
    Dim myInfoEvent As EventInstance = new EventInstance(InformationMsgId, 0, EventLogEntryType.Information)
    Dim myWarningEvent As EventInstance = new EventInstance(WarningMsgId, 0, EventLogEntryType.Warning)

    ' Insert the method name into the event log message.
    Dim insertStrings() As String = {"EventLogSamples.WriteEventSample2"}

    ' Write the events to the event log.

    EventLog.WriteEvent(sourceName, myInfoEvent, insertStrings)

    ' Append binary data to the warning event entry.
    Dim binaryData() As Byte = { 7, 8, 9, 10 }
    EventLog.WriteEvent(sourceName, myWarningEvent, binaryData, insertStrings)
Else 
    Console.WriteLine("Warning - event source {0} not registered", _
        sourceName)
End If

此範例會使用內建于資源連結庫 EventLogMsgs 的下列郵件內文檔案。The example uses the following message text file, built into the resource library EventLogMsgs.dll. 訊息文字檔是用來建立訊息資源檔的來源。A message text file is the source from which the message resource file is created. 訊息文字檔會定義類別目錄、事件訊息和參數插入字串的資源識別碼和文字。The message text file defines the resource identifiers and text for the category, event message, and parameter insertion strings.

; // 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  
.  

備註

使用此方法,將當地語系化的專案寫入事件記錄檔,並使用已註冊為適當記錄檔之事件來源的來源。Use this method to write a localized entry to the event log, using a source already registered as an event source for the appropriate log. 您可以使用資源識別碼來指定事件屬性,而不是字串值。You specify the event properties with resource identifiers rather than string values. 事件檢視器會使用資源識別碼,從來源的當地語系化資源檔顯示對應的字串。The Event Viewer uses the resource identifiers to display the corresponding strings from the localized resource file for the source. 您必須先向對應的資源檔登錄來源,才能使用資源識別碼來撰寫事件。You must register the source with the corresponding resource file before you write events using resource identifiers.

輸入instance實例會指定事件訊息和屬性。The input instance instance specifies the event message and properties. 針對來源訊息資源檔中已定義的訊息,設定其輸入的。instance InstanceIdSet the InstanceId of the instance input for the defined message in the source message resource file. 您可以選擇性地設定CategoryId instance輸入EntryType的和,以定義事件專案的分類和事件種類。You can optionally set the CategoryId and EntryType of the instance input to define the category and event type of your event entry. 您也可以指定與語言無關的字串陣列,以插入當地語系化的郵件內文中。You can also specify an array of language-independent strings to insert into the localized message text. 如果values事件null訊息不包含取代字串的格式預留位置,則設定為。Set values to null if the event message does not contain formatting placeholders for replacement strings.

在使用WriteEvent之前,必須先註冊事件記錄檔的指定來源。The specified source must be registered for an event log before using WriteEvent. 必須設定指定的來源,以便將當地語系化的專案寫入記錄檔;來源至少必須定義訊息資源檔。The specified source must be configured for writing localized entries to the log; the source must at minimum have a message resource file defined.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。You must create and configure the event source before writing the first entry with the source. 在應用程式安裝期間建立新的事件來源。Create the new event source during the installation of your application. 這可讓作業系統重新整理其已註冊事件來源清單及其設定。This allows time for the operating system to refresh its list of registered event sources and their configuration. 如果作業系統尚未重新整理其事件來源清單,而且您嘗試使用新的來源寫入事件,寫入作業將會失敗。If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. 您可以使用EventLogInstaller來設定新的來源,或CreateEventSource使用方法。You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. 您必須具有電腦的系統管理許可權,才能建立新的事件來源。You must have administrative rights on the computer to create a new event source.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 如果您WriteEntry的應用程式會將字串值直接寫入事件記錄檔,請使用方法。Use the WriteEntry method if your application writes string values directly to the event log.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。If your application writes entries using both resource identifiers and string values, you must register two separate sources. 例如,設定一個具有資源檔的來源,然後在WriteEvent方法中使用該來源,將使用資源識別碼的專案寫入事件記錄檔。For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. 然後建立不含資源檔的不同來源,然後在WriteEntry方法中使用該來源,將字串直接寫入至使用該來源的事件記錄檔。Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

安全性

EventLogPermission
用於寫入電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉:WriteAssociated enumeration: Write

另請參閱

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

將當地語系化的項目寫入事件記錄檔。Writes a localized entry to the event log.

public:
 void WriteEvent(System::Diagnostics::EventInstance ^ instance, ... cli::array <System::Object ^> ^ values);
[System.Runtime.InteropServices.ComVisible(false)]
public void WriteEvent (System.Diagnostics.EventInstance instance, params object[] values);
member this.WriteEvent : System.Diagnostics.EventInstance * obj[] -> unit
Public Sub WriteEvent (instance As EventInstance, ParamArray values As Object())

參數

instance
EventInstance EventInstance EventInstance EventInstance

EventInstance 執行個體,表示當地語系化的事件記錄項目。An EventInstance instance that represents a localized event log entry.

values
Object[]

要合併到事件記錄項目訊息文字的字串陣列。An array of strings to merge into the message text of the event log entry.

例外狀況

還沒有設定 SourceEventLog 屬性。The Source property of the EventLog has not been set.

-或--or- 該方法已嘗試登錄新的事件來源,但 MachineName 中的電腦名稱無效。The method attempted to register a new event source, but the computer name in MachineName is not valid.

-或--or- 來源已經登錄用於不同的事件記錄檔。The source is already registered for a different event log.

-或--or- instance.InstanceId 小於零或大於 MaxValueinstance.InstanceId is less than zero or greater than MaxValue.

-或--or- values 擁有超過 256 個項目。values has more than 256 elements.

-或--or- 其中一個 values 項目的長度超過 32766 個位元組。One of the values elements is longer than 32766 bytes.

-或--or- 來源名稱產生的登錄機碼路徑長度超過 254 個字元。The source name results in a registry key path longer than 254 characters.

無法開啟事件記錄檔的登錄機碼。The registry key for the event log could not be opened.

作業系統在將事件項目寫入事件記錄檔時回報錯誤。The operating system reported an error when writing the event entry to the event log. 無法使用 Windows 錯誤碼。A Windows error code is not available.

範例

下列範例會將兩個 audit 專案寫入事件記錄myNewLog檔。The following example writes two audit entries to the event log myNewLog. 此範例會建立新的事件來源,以及新的事件記錄檔(如果它們不存在於本機電腦上)。The example creates a new event source and a new event log if they do not exist on the local computer. 事件郵件內文是使用資源檔中的資源識別碼所指定。The event message text is specified using a resource identifier in a resource file.

// 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 = gcnew 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 = gcnew EventInstance( AuditSuccessMsgId,0,EventLogEntryType::SuccessAudit );
EventInstance ^ myAuditFailEvent = gcnew EventInstance( AuditFailedMsgId,0,EventLogEntryType::FailureAudit );

// Insert the method name into the event log message.
array<String^>^insertStrings = {"EventLogSamples.WriteEventSample1"};

// Write the events to the event log.
myEventLog->WriteEvent( myAuditSuccessEvent, insertStrings );

// Append binary data to the audit failure event entry.
array<Byte>^binaryData = {3,4,5,6};
myEventLog->WriteEvent( myAuditFailEvent, binaryData, insertStrings );


// 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); 


Dim sourceName As String = "SampleApplicationSource"

' Create the event source if it does not exist.
If Not 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.

    Dim messageFile As String =  String.Format("{0}\\{1}", _
        System.Environment.CurrentDirectory, _
        "EventLogMsgs.dll")

    CreateEventSourceSample1(messageFile)
End If 

' Get the event log corresponding to the existing source.
Dim myLogName As String = EventLog.LogNameFromSourceName(sourceName,".")

Dim myEventLog As EventLog = new EventLog(myLogName, ".", sourceName)

' Define two audit events.
Dim myAuditSuccessEvent As EventInstance = new EventInstance(AuditSuccessMsgId, 0, EventLogEntryType.SuccessAudit)
Dim myAuditFailEvent As EventInstance = new EventInstance(AuditFailedMsgId, 0, EventLogEntryType.FailureAudit)

' Insert the method name into the event log message.
Dim insertStrings() As String = {"EventLogSamples.WriteEventSample1"}

' Write the events to the event log.

myEventLog.WriteEvent(myAuditSuccessEvent, insertStrings)

' Append binary data to the audit failure event entry.
Dim binaryData() As Byte = { 7, 8, 9, 10 }
myEventLog.WriteEvent(myAuditFailEvent, binaryData, insertStrings)

此範例會使用內建于資源連結庫 EventLogMsgs 的下列郵件內文檔案。The example uses the following message text file, built into the resource library EventLogMsgs.dll. 訊息文字檔是用來建立訊息資源檔的來源。A message text file is the source from which the message resource file is created. 訊息文字檔會定義類別目錄、事件訊息和參數插入字串的資源識別碼和文字。The message text file defines the resource identifiers and text for the category, event message, and parameter insertion strings.

; // 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  
.  

備註

使用此方法,將當地語系化的專案寫入事件記錄檔。Use this method to write a localized entry to the event log. 您可以使用資源識別碼來指定事件屬性,而不是字串值。You specify the event properties with resource identifiers rather than string values. 事件檢視器會使用資源識別碼,從的當地語系化資源檔Source顯示對應的字串。The Event Viewer uses the resource identifiers to display the corresponding strings from the localized resource file for the Source. 您必須先向對應的資源檔登錄來源,才能使用資源識別碼來撰寫事件。You must register the source with the corresponding resource file before you write events using resource identifiers.

輸入instance實例會指定事件訊息和屬性。The input instance instance specifies the event message and properties. 針對來源訊息資源檔中已定義的訊息,設定其輸入的。instance InstanceIdSet the InstanceId of the instance input for the defined message in the source message resource file. 您可以選擇性地設定CategoryId instance輸入EntryType的和,以定義事件專案的分類和事件種類。You can optionally set the CategoryId and EntryType of the instance input to define the category and event type of your event entry. 您也可以指定與語言無關的字串陣列,以插入當地語系化的郵件內文中。You can also specify an array of language-independent strings to insert into the localized message text. 如果values事件null訊息不包含取代字串的格式預留位置,則設定為。Set values to null if the event message does not contain formatting placeholders for replacement strings.

您必須先EventLogSource元件上設定屬性,才能WriteEvent使用。You must set the Source property on your EventLog component before using WriteEvent. 必須設定指定的來源,以便將當地語系化的專案寫入記錄檔;來源至少必須定義訊息資源檔。The specified source must be configured for writing localized entries to the log; the source must at minimum have a message resource file defined.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。You must create and configure the event source before writing the first entry with the source. 在應用程式安裝期間建立新的事件來源。Create the new event source during the installation of your application. 這可讓作業系統重新整理其已註冊事件來源清單及其設定。This allows time for the operating system to refresh its list of registered event sources and their configuration. 如果作業系統尚未重新整理其事件來源清單,而且您嘗試使用新的來源寫入事件,寫入作業將會失敗。If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. 您可以使用EventLogInstaller來設定新的來源,或CreateEventSource使用方法。You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. 您必須具有電腦的系統管理許可權,才能建立新的事件來源。You must have administrative rights on the computer to create a new event source.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 如果您WriteEntry的應用程式會將字串值直接寫入事件記錄檔,請使用方法。Use the WriteEntry method if your application writes string values directly to the event log.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。If your application writes entries using both resource identifiers and string values, you must register two separate sources. 例如,設定一個具有資源檔的來源,然後在WriteEvent方法中使用該來源,將使用資源識別碼的專案寫入事件記錄檔。For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. 然後建立不含資源檔的不同來源,然後在WriteEntry方法中使用該來源,將字串直接寫入至使用該來源的事件記錄檔。Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

注意

如果您將專案寫入遠端電腦,則如果遠端電腦未執行message .NET Framework,字串的值可能就不是您所預期的值。If you write an entry to a remote computer, the value of the message string might not be what you expect if the remote computer is not running the .NET Framework. 此外,此message字串不能包含%n,其中n是整數值(例如% 1),因為事件檢視器會將它視為插入字串。Also, the message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. 因為網際網路通訊協定第6版(IPv6)位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

安全性

EventLogPermission
用於寫入電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉:WriteAssociated enumeration: Write

另請參閱

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

以指定的事件資料、訊息取代字串和相關聯的二進位資料來寫入事件記錄檔項目。Writes an event log entry with the given event data, message replacement strings, and associated binary data.

public:
 void WriteEvent(System::Diagnostics::EventInstance ^ instance, cli::array <System::Byte> ^ data, ... cli::array <System::Object ^> ^ values);
[System.Runtime.InteropServices.ComVisible(false)]
public void WriteEvent (System.Diagnostics.EventInstance instance, byte[] data, params object[] values);
member this.WriteEvent : System.Diagnostics.EventInstance * byte[] * obj[] -> unit
Public Sub WriteEvent (instance As EventInstance, data As Byte(), ParamArray values As Object())

參數

instance
EventInstance EventInstance EventInstance EventInstance

EventInstance 執行個體,表示當地語系化的事件記錄項目。An EventInstance instance that represents a localized event log entry.

data
Byte[]

保存與項目相關的二進位資料的位元組陣列。An array of bytes that holds the binary data associated with the entry.

values
Object[]

要合併到事件記錄項目訊息文字的字串陣列。An array of strings to merge into the message text of the event log entry.

例外狀況

還沒有設定 SourceEventLog 屬性。The Source property of the EventLog has not been set.

-或--or- 該方法已嘗試登錄新的事件來源,但 MachineName 中的電腦名稱無效。The method attempted to register a new event source, but the computer name in MachineName is not valid.

-或--or- 來源已經登錄用於不同的事件記錄檔。The source is already registered for a different event log.

-或--or- instance.InstanceId 小於零或大於 MaxValueinstance.InstanceId is less than zero or greater than MaxValue.

-或--or- values 擁有超過 256 個項目。values has more than 256 elements.

-或--or- 其中一個 values 項目的長度超過 32766 個位元組。One of the values elements is longer than 32766 bytes.

-或--or- 來源名稱產生的登錄機碼路徑長度超過 254 個字元。The source name results in a registry key path longer than 254 characters.

無法開啟事件記錄檔的登錄機碼。The registry key for the event log could not be opened.

作業系統在將事件項目寫入事件記錄檔時回報錯誤。The operating system reported an error when writing the event entry to the event log. 無法使用 Windows 錯誤碼。A Windows error code is not available.

範例

下列範例會將兩個 audit 專案寫入事件記錄myNewLog檔。The following example writes two audit entries to the event log myNewLog. 此範例會建立新的事件來源,以及新的事件記錄檔(如果它們不存在於本機電腦上)。The example creates a new event source and a new event log if they do not exist on the local computer. 事件郵件內文是使用資源檔中的資源識別碼所指定。The event message text is specified using a resource identifier in a resource file.

// 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 = gcnew 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 = gcnew EventInstance( AuditSuccessMsgId,0,EventLogEntryType::SuccessAudit );
EventInstance ^ myAuditFailEvent = gcnew EventInstance( AuditFailedMsgId,0,EventLogEntryType::FailureAudit );

// Insert the method name into the event log message.
array<String^>^insertStrings = {"EventLogSamples.WriteEventSample1"};

// Write the events to the event log.
myEventLog->WriteEvent( myAuditSuccessEvent, insertStrings );

// Append binary data to the audit failure event entry.
array<Byte>^binaryData = {3,4,5,6};
myEventLog->WriteEvent( myAuditFailEvent, binaryData, insertStrings );


// 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); 


Dim sourceName As String = "SampleApplicationSource"

' Create the event source if it does not exist.
If Not 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.

    Dim messageFile As String =  String.Format("{0}\\{1}", _
        System.Environment.CurrentDirectory, _
        "EventLogMsgs.dll")

    CreateEventSourceSample1(messageFile)
End If 

' Get the event log corresponding to the existing source.
Dim myLogName As String = EventLog.LogNameFromSourceName(sourceName,".")

Dim myEventLog As EventLog = new EventLog(myLogName, ".", sourceName)

' Define two audit events.
Dim myAuditSuccessEvent As EventInstance = new EventInstance(AuditSuccessMsgId, 0, EventLogEntryType.SuccessAudit)
Dim myAuditFailEvent As EventInstance = new EventInstance(AuditFailedMsgId, 0, EventLogEntryType.FailureAudit)

' Insert the method name into the event log message.
Dim insertStrings() As String = {"EventLogSamples.WriteEventSample1"}

' Write the events to the event log.

myEventLog.WriteEvent(myAuditSuccessEvent, insertStrings)

' Append binary data to the audit failure event entry.
Dim binaryData() As Byte = { 7, 8, 9, 10 }
myEventLog.WriteEvent(myAuditFailEvent, binaryData, insertStrings)

此範例會使用內建于資源連結庫 EventLogMsgs 的下列郵件內文檔案。The example uses the following message text file, built into the resource library EventLogMsgs.dll. 訊息文字檔是用來建立訊息資源檔的來源。A message text file is the source from which the message resource file is created. 訊息文字檔會定義類別目錄、事件訊息和參數插入字串的資源識別碼和文字。The message text file defines the resource identifiers and text for the category, event message, and parameter insertion strings.

; // 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  
.  

備註

使用此方法,將具有其他事件特定資料的當地語系化專案寫入事件記錄檔。Use this method to write a localized entry with additional event-specific data to the event log. 您可以使用資源識別碼來指定事件屬性,而不是字串值。You specify the event properties with resource identifiers rather than string values. 事件檢視器會使用資源識別碼,從的當地語系化資源檔Source顯示對應的字串。The Event Viewer uses the resource identifiers to display the corresponding strings from the localized resource file for the Source. 您必須先向對應的資源檔登錄來源,才能使用資源識別碼來撰寫事件。You must register the source with the corresponding resource file before you write events using resource identifiers.

輸入instance實例會指定事件訊息和屬性。The input instance instance specifies the event message and properties. 針對來源訊息資源檔中已定義的訊息,設定其輸入的。instance InstanceIdSet the InstanceId of the instance input for the defined message in the source message resource file. 您可以選擇性地設定CategoryId instance輸入EntryType的和,以定義事件專案的分類和事件種類。You can optionally set the CategoryId and EntryType of the instance input to define the category and event type of your event entry. 您也可以指定與語言無關的字串陣列,以插入當地語系化的郵件內文中。You can also specify an array of language-independent strings to insert into the localized message text. 如果values事件null訊息不包含取代字串的格式預留位置,則設定為。Set values to null if the event message does not contain formatting placeholders for replacement strings.

當需要提供事件的其他詳細資料時,請指定具有事件的二進位資料。Specify binary data with an event when it is necessary to provide additional details for the event. 例如,您可以使用data參數來包含有關特定錯誤的資訊。For example, use the data parameter to include information on a specific error. 事件檢視器不會解讀相關聯的事件資料;它會以結合的十六進位和文字格式來顯示資料。The Event Viewer does not interpret the associated event data; it displays the data in a combined hexadecimal and text format. 謹慎使用事件特定資料;只有在您確定它會很有用時,才包含此功能。Use event-specific data sparingly; include it only if you are sure it will be useful. 您也可以使用事件特定的資料來儲存應用程式可以獨立處理事件檢視器的資訊。You can also use event-specific data to store information the application can process independently of the Event Viewer. 例如,您可以為事件撰寫特別的檢視器,或撰寫一個程式來掃描事件記錄檔,並建立包含事件特定資料資訊的報表。For example, you could write a viewer specifically for your events, or write a program that scans the event log and creates reports that include information from the event-specific data.

在使用Source EventLog 之前,您必須先在元件之前設定WriteEvent元件的屬性。You must set the Source property on your EventLog component before component before using WriteEvent. 必須設定指定的來源,以便將當地語系化的專案寫入記錄檔;來源至少必須定義訊息資源檔。The specified source must be configured for writing localized entries to the log; the source must at minimum have a message resource file defined.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。You must create and configure the event source before writing the first entry with the source. 在應用程式安裝期間建立新的事件來源。Create the new event source during the installation of your application. 這可讓作業系統重新整理其已註冊事件來源清單及其設定。This allows time for the operating system to refresh its list of registered event sources and their configuration. 如果作業系統尚未重新整理其事件來源清單,而且您嘗試使用新的來源寫入事件,寫入作業將會失敗。If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. 您可以使用EventLogInstaller來設定新的來源,或CreateEventSource使用方法。You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. 您必須具有電腦的系統管理許可權,才能建立新的事件來源。You must have administrative rights on the computer to create a new event source.

注意

如果您未在呼叫MachineName WriteEvent之前EventLog為實例指定,則會假設使用本機電腦(".")。If you do not specify a MachineName for your EventLog instance before you call WriteEvent, the local computer (".") is assumed.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 如果您WriteEntry的應用程式會將字串值直接寫入事件記錄檔,請使用方法。Use the WriteEntry method if your application writes string values directly to the event log.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。If your application writes entries using both resource identifiers and string values, you must register two separate sources. 例如,設定一個具有資源檔的來源,然後在WriteEvent方法中使用該來源,將使用資源識別碼的專案寫入事件記錄檔。For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. 然後建立不含資源檔的不同來源,然後在WriteEntry方法中使用該來源,將字串直接寫入至使用該來源的事件記錄檔。Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

注意

如果您將專案寫入遠端電腦,則如果遠端電腦未執行message .NET Framework,字串的值可能就不是您所預期的值。If you write an entry to a remote computer, the value of the message string might not be what you expect if the remote computer is not running the .NET Framework. 此外,此message字串不能包含%n,其中n是整數值(例如% 1),因為事件檢視器會將它視為插入字串。Also, the message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. 因為網際網路通訊協定第6版(IPv6)位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address.

安全性

EventLogPermission
用於寫入電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉:WriteAssociated enumeration: Write

另請參閱

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

以指定的事件資料、訊息取代字串和相關聯的二進位資料,以及使用指定的已登錄事件來源,寫入事件記錄檔項目。Writes an event log entry with the given event data, message replacement strings, and associated binary data, and using the specified registered event source.

public:
 static void WriteEvent(System::String ^ source, System::Diagnostics::EventInstance ^ instance, cli::array <System::Byte> ^ data, ... cli::array <System::Object ^> ^ values);
public static void WriteEvent (string source, System.Diagnostics.EventInstance instance, byte[] data, params object[] values);
static member WriteEvent : string * System.Diagnostics.EventInstance * byte[] * obj[] -> unit
Public Shared Sub WriteEvent (source As String, instance As EventInstance, data As Byte(), ParamArray values As Object())

參數

source
String String String String

為指定電腦上的應用程式註冊的事件來源名稱。The name of the event source registered for the application on the specified computer.

instance
EventInstance EventInstance EventInstance EventInstance

EventInstance 執行個體,表示當地語系化的事件記錄項目。An EventInstance instance that represents a localized event log entry.

data
Byte[]

保存與項目相關的二進位資料的位元組陣列。An array of bytes that holds the binary data associated with the entry.

values
Object[]

要合併到事件記錄項目訊息文字的字串陣列。An array of strings to merge into the message text of the event log entry.

例外狀況

source 值是空白字串 ("")。The source value is an empty string ("").

-或--or- source 值為 nullThe source value is null.

-或--or- instance.InstanceId 小於零或大於 MaxValueinstance.InstanceId is less than zero or greater than MaxValue.

-或--or- values 擁有超過 256 個項目。values has more than 256 elements.

-或--or- 其中一個 values 項目的長度超過 32766 個位元組。One of the values elements is longer than 32766 bytes.

-或--or- 來源名稱產生的登錄機碼路徑長度超過 254 個字元。The source name results in a registry key path longer than 254 characters.

無法開啟事件記錄檔的登錄機碼。The registry key for the event log could not be opened.

作業系統在將事件項目寫入事件記錄檔時回報錯誤。The operating system reported an error when writing the event entry to the event log. 無法使用 Windows 錯誤碼。A Windows error code is not available.

範例

下列範例會將參考事件專案和警告事件專案寫入現有的事件記錄檔。The following example writes an informational event entry and a warning event entry to an existing event log. 事件郵件內文是使用資源檔中的資源識別碼所指定。The event message text is specified using a resource identifier in a resource file. 此範例假設對應的資源檔已針對來源註冊。The example assumes the corresponding resource file has been registered for the source.

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 = gcnew EventInstance( InformationMsgId,0,EventLogEntryType::Information );
   EventInstance ^ myWarningEvent = gcnew EventInstance( WarningMsgId,0,EventLogEntryType::Warning );
   
   // Insert the method name into the event log message.
   array<String^>^insertStrings = {"EventLogSamples.WriteEventSample2"};
   
   // Write the events to the event log.
   EventLog::WriteEvent( sourceName, myInfoEvent, 0 );
   
   // Append binary data to the warning event entry.
   array<Byte>^binaryData = {7,8,9,10};
   EventLog::WriteEvent( sourceName, myWarningEvent, binaryData, insertStrings );
}
else
{
   Console::WriteLine( "Warning - event source {0} not registered", sourceName );
}

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);
}
Dim sourceName As String = "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.
    Dim myInfoEvent As EventInstance = new EventInstance(InformationMsgId, 0, EventLogEntryType.Information)
    Dim myWarningEvent As EventInstance = new EventInstance(WarningMsgId, 0, EventLogEntryType.Warning)

    ' Insert the method name into the event log message.
    Dim insertStrings() As String = {"EventLogSamples.WriteEventSample2"}

    ' Write the events to the event log.

    EventLog.WriteEvent(sourceName, myInfoEvent, insertStrings)

    ' Append binary data to the warning event entry.
    Dim binaryData() As Byte = { 7, 8, 9, 10 }
    EventLog.WriteEvent(sourceName, myWarningEvent, binaryData, insertStrings)
Else 
    Console.WriteLine("Warning - event source {0} not registered", _
        sourceName)
End If

此範例會使用內建于資源連結庫 EventLogMsgs 的下列郵件內文檔案。The example uses the following message text file, built into the resource library EventLogMsgs.dll. 訊息文字檔是用來建立訊息資源檔的來源。A message text file is the source from which the message resource file is created. 訊息文字檔會定義類別目錄、事件訊息和參數插入字串的資源識別碼和文字。The message text file defines the resource identifiers and text for the category, event message, and parameter insertion strings.

; // 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  
.  

備註

使用此方法,將具有其他事件特定資料的當地語系化專案寫入事件記錄檔中,使用已註冊為適當記錄檔之事件來源的來源。Use this method to write a localized entry with additional event-specific data to the event log, using a source already registered as an event source for the appropriate log. 您可以使用資源識別碼來指定事件屬性,而不是字串值。You specify the event properties with resource identifiers rather than string values. 事件檢視器會使用資源識別碼,從來源的當地語系化資源檔顯示對應的字串。The Event Viewer uses the resource identifiers to display the corresponding strings from the localized resource file for the source. 您必須先向對應的資源檔登錄來源,才能使用資源識別碼來撰寫事件。You must register the source with the corresponding resource file before you write events using resource identifiers.

輸入instance實例會指定事件訊息和屬性。The input instance instance specifies the event message and properties. 針對來源訊息資源檔中已定義的訊息,設定其輸入的。instance InstanceIdSet the InstanceId of the instance input for the defined message in the source message resource file. 您可以選擇性地設定CategoryId instance輸入EntryType的和,以定義事件專案的分類和事件種類。You can optionally set the CategoryId and EntryType of the instance input to define the category and event type of your event entry. 您也可以指定與語言無關的字串陣列,以插入當地語系化的郵件內文中。You can also specify an array of language-independent strings to insert into the localized message text. 如果values事件null訊息不包含取代字串的格式預留位置,則設定為。Set values to null if the event message does not contain formatting placeholders for replacement strings.

當需要提供事件的其他詳細資料時,請指定具有事件的二進位資料。Specify binary data with an event when it is necessary to provide additional details for the event. 例如,您可以使用data參數來包含有關特定錯誤的資訊。For example, use the data parameter to include information on a specific error. 事件檢視器不會解讀相關聯的事件資料;它會以結合的十六進位和文字格式來顯示資料。The Event Viewer does not interpret the associated event data; it displays the data in a combined hexadecimal and text format. 謹慎使用事件特定資料;只有在您確定它會很有用時,才包含此功能。Use event-specific data sparingly; include it only if you are sure it will be useful. 您也可以使用事件特定的資料來儲存應用程式可以獨立處理事件檢視器的資訊。You can also use event-specific data to store information the application can process independently of the Event Viewer. 例如,您可以為事件撰寫特別的檢視器,或撰寫一個程式來掃描事件記錄檔,並建立包含事件特定資料資訊的報表。For example, you could write a viewer specifically for your events, or write a program that scans the event log and creates reports that include information from the event-specific data.

在使用WriteEvent之前,必須先註冊事件記錄檔的指定來源。The specified source must be registered for an event log before using WriteEvent. 必須設定指定的來源,以便將當地語系化的專案寫入記錄檔;來源至少必須定義訊息資源檔。The specified source must be configured for writing localized entries to the log; the source must at minimum have a message resource file defined.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。You must create and configure the event source before writing the first entry with the source. 在應用程式安裝期間建立新的事件來源。Create the new event source during the installation of your application. 這可讓作業系統重新整理其已註冊事件來源清單及其設定。This allows time for the operating system to refresh its list of registered event sources and their configuration. 如果作業系統尚未重新整理其事件來源清單,而且您嘗試使用新的來源寫入事件,寫入作業將會失敗。If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail. 您可以使用EventLogInstaller來設定新的來源,或CreateEventSource使用方法。You can configure a new source using an EventLogInstaller, or using the CreateEventSource method. 您必須具有電腦的系統管理許可權,才能建立新的事件來源。You must have administrative rights on the computer to create a new event source.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 如果您WriteEntry的應用程式會將字串值直接寫入事件記錄檔,請使用方法。Use the WriteEntry method if your application writes string values directly to the event log.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。If your application writes entries using both resource identifiers and string values, you must register two separate sources. 例如,設定一個具有資源檔的來源,然後在WriteEvent方法中使用該來源,將使用資源識別碼的專案寫入事件記錄檔。For example, configure one source with resource files, and then use that source in the WriteEvent method to write entries using resource identifiers to the event log. 然後建立不含資源檔的不同來源,然後在WriteEntry方法中使用該來源,將字串直接寫入至使用該來源的事件記錄檔。Then create a different source without resource files, and use that source in the WriteEntry method to write strings directly to the event log using that source.

安全性

EventLogPermission
用於寫入電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉:WriteAssociated enumeration: Write

另請參閱

適用於