EventLog.WriteEntry EventLog.WriteEntry EventLog.WriteEntry EventLog.WriteEntry Method

定義

將項目寫入事件記錄檔中。Writes an entry in the event log.

多載

WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[])

將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔 (使用指定的登錄事件來源),並將二進位資料附加到訊息。Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log (using the specified registered event source) and appends binary data to the message.

WriteEntry(String, String, EventLogEntryType, Int32, Int16) WriteEntry(String, String, EventLogEntryType, Int32, Int16) WriteEntry(String, String, EventLogEntryType, Int32, Int16) WriteEntry(String, String, EventLogEntryType, Int32, Int16)

使用指定的登錄事件來源,將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔。Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, using the specified registered event source. category 可以讓事件檢視器用來篩選記錄檔中的事件。The category can be used by the Event Viewer to filter events in the log.

WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[])

將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔,並將二進位資料附加到訊息。Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, and appends binary data to the message.

WriteEntry(String, String, EventLogEntryType, Int32) WriteEntry(String, String, EventLogEntryType, Int32) WriteEntry(String, String, EventLogEntryType, Int32) WriteEntry(String, String, EventLogEntryType, Int32)

使用指定的登錄事件來源,將有指定訊息文字和應用程式定義的事件識別項的項目寫入事件記錄檔。Writes an entry with the given message text and application-defined event identifier to the event log, using the specified registered event source.

WriteEntry(String, EventLogEntryType, Int32, Int16) WriteEntry(String, EventLogEntryType, Int32, Int16) WriteEntry(String, EventLogEntryType, Int32, Int16) WriteEntry(String, EventLogEntryType, Int32, Int16)

將具有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔。Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log.

WriteEntry(String, EventLogEntryType) WriteEntry(String, EventLogEntryType) WriteEntry(String, EventLogEntryType) WriteEntry(String, EventLogEntryType)

將具有指定訊息文字的錯誤、警告、資訊、成功稽核或失敗稽核項目寫入事件記錄檔。Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log.

WriteEntry(String, EventLogEntryType, Int32) WriteEntry(String, EventLogEntryType, Int32) WriteEntry(String, EventLogEntryType, Int32) WriteEntry(String, EventLogEntryType, Int32)

將有指定訊息文字和應用程式定義的事件識別項的項目寫入事件記錄檔。Writes an entry with the given message text and application-defined event identifier to the event log.

WriteEntry(String, String) WriteEntry(String, String) WriteEntry(String, String) WriteEntry(String, String)

使用指定的登錄事件來源,將具有指定訊息文字的資訊類型項目寫入事件記錄檔。Writes an information type entry with the given message text to the event log, using the specified registered event source.

WriteEntry(String) WriteEntry(String) WriteEntry(String) WriteEntry(String)

將具有指定訊息文字的資訊類型項目寫入事件記錄檔。Writes an information type entry, with the given message text, to the event log.

WriteEntry(String, String, EventLogEntryType) WriteEntry(String, String, EventLogEntryType) WriteEntry(String, String, EventLogEntryType) WriteEntry(String, String, EventLogEntryType)

用指定的登錄事件來源,將有指定訊息文字的錯誤、警告、資訊、成功稽核或失敗稽核項目寫入事件記錄檔。Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log, using the specified registered event source.

WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, String, EventLogEntryType, Int32, Int16, Byte[])

將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔 (使用指定的登錄事件來源),並將二進位資料附加到訊息。Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log (using the specified registered event source) and appends binary data to the message.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID, short category, cli::array <System::Byte> ^ rawData);
public static void WriteEntry (string source, string message, System.Diagnostics.EventLogEntryType type, int eventID, short category, byte[] rawData);
static member WriteEntry : string * string * System.Diagnostics.EventLogEntryType * int * int16 * byte[] -> unit
Public Shared Sub WriteEntry (source As String, message As String, type As EventLogEntryType, eventID As Integer, category As Short, rawData As Byte())

參數

source
String String String String

將應用程式註冊在指定電腦上的來源。The source by which the application is registered on the specified computer.

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

eventID
Int32 Int32 Int32 Int32

事件的應用程式特定識別項。The application-specific identifier for the event.

category
Int16 Int16 Int16 Int16

與訊息相關的應用程式特定的子分類。The application-specific subcategory associated with the message.

rawData
Byte[]

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

例外狀況

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

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

-或--or- eventID 小於零或大於 MaxValueeventID is less than zero or greater than MaxValue.

-或--or- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

//Create a byte array for binary data to associate with the entry.
array<Byte>^myByte = gcnew array<Byte>(10);
//Populate the byte array with simulated data.
for ( int i = 0; i < 10; i++ )
{
   myByte[ i ] = (Byte)(i % 2);
}
//Write an entry to the event log that includes associated binary data.
Console::WriteLine( "Write from second source " );
EventLog::WriteEntry( "SecondSource", "Writing warning to event log.",
   EventLogEntryType::Error, myEventID, myCategory, myByte );
//Create a byte array for binary data to associate with the entry.
byte[] myByte = new byte[10];
//Populate the byte array with simulated data.
for (int i = 0; i < 10; i++)
{
    myByte[i] = (byte)(i % 2);
}
// Write an entry to the event log that includes associated binary data.
Console.WriteLine("Write from second source ");
EventLog.WriteEntry("SecondSource", "Writing warning to event log.",
                     EventLogEntryType.Error, myEventID, myCategory, myByte);
' Create a byte array for binary data to associate with the entry.
Dim myByte(9) As Byte
Dim i As Integer
' Populate the byte array with simulated data.
For i = 0 To 9
    myByte(i) = CByte(i Mod 2)
Next i
' Write an entry to the event log that includes associated binary data.
Console.WriteLine("Write from second source ")
EventLog.WriteEntry("SecondSource", "Writing warning to event log.", _
                     EventLogEntryType.Error, myEventID, myCategory, myByte)

備註

使用此方法,將應用程式定義的事件特定資料寫入事件記錄檔,使用已註冊為適當記錄檔之事件來源的來源。Use this method to write application-defined event-specific data to the event log, using a source already registered as an event source for the appropriate log. 事件檢視器不會解讀此資料;它只會以結合的十六進位和文字格式顯示原始資料。The Event Viewer does not interpret this data; it displays raw data only 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 logfile and creates reports that include information from the event-specific data.

除了二進位資料以外,您還可以指定應用程式定義的分類和應用程式定義的事件識別碼。In addition to the binary data, you can specify an application-defined category and an application-defined event identifier. 事件檢視器使用類別來篩選事件來源所寫入的事件。The Event Viewer uses the category to filter events written by an event source. 事件檢視器可以將類別顯示為數值,也可以使用類別做為資源識別碼來顯示當地語系化的類別字串。The Event Viewer can display the category as a numeric value, or it can use the category as a resource identifier to display a localized category string.

注意

category參數應該是正數值。The category parameter should be a positive value. 負值類別目錄值會在事件檢視器中顯示為互補的正數。Negative category values appear as a complementary positive number in the Event Viewer. 例如,-10 會顯示為65526,即-1,表示為65535。For example, a -10 will appear as 65,526, a -1 as 65,535.

若要在事件檢視器中顯示當地語系化的分類字串,您必須使用以類別資源檔設定的category事件來源,並將設為分類資源檔中的資源識別碼。To display localized category strings in the Event Viewer, you must use an event source configured with a category resource file, and set the category to a resource identifier in the category resource file. 如果事件來源沒有已設定的分類資源檔,或指定category的並未為分類資源檔中的字串編制索引,則事件檢視器會顯示該專案的數值類別目錄值。If the event source does not have a configured category resource file, or the specified category does not index a string in the category resource file, then the Event Viewer displays the numeric category value for that entry. EventLogInstaller 使用EventSourceCreationData或類別,設定類別資源檔,以及資源檔中的類別目錄字串數目。Configure the category resource file, along with the number of category strings in the resource file, using the EventLogInstaller or the EventSourceCreationData class.

事件識別碼,以及事件來源,可唯一識別事件。Event identifiers, together with the event source, uniquely identify an event. 每個應用程式都可以定義自己的編號事件和它們所對應的描述字串。Each application can define its own numbered events and the description strings to which they map. 事件檢視器會顯示這些字串值,以協助使用者瞭解發生了什麼錯誤,並建議採取哪些動作。Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

最後,您可以指定EventLogEntryType將事件寫入事件記錄檔的。Finally, you can specify an EventLogEntryType for the event being written to the event log. type是以記錄檔的事件檢視器中類型資料行內的圖示和文字來表示。The type is indicated by an icon and text in the Type column in the Event Viewer for a log. 此參數會指出事件種類為錯誤、警告、資訊、成功 audit 或失敗 audit。This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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

另請參閱

WriteEntry(String, String, EventLogEntryType, Int32, Int16) WriteEntry(String, String, EventLogEntryType, Int32, Int16) WriteEntry(String, String, EventLogEntryType, Int32, Int16) WriteEntry(String, String, EventLogEntryType, Int32, Int16)

使用指定的登錄事件來源,將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔。Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, using the specified registered event source. category 可以讓事件檢視器用來篩選記錄檔中的事件。The category can be used by the Event Viewer to filter events in the log.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID, short category);
public static void WriteEntry (string source, string message, System.Diagnostics.EventLogEntryType type, int eventID, short category);
static member WriteEntry : string * string * System.Diagnostics.EventLogEntryType * int * int16 -> unit
Public Shared Sub WriteEntry (source As String, message As String, type As EventLogEntryType, eventID As Integer, category As Short)

參數

source
String String String String

將應用程式註冊在指定電腦上的來源。The source by which the application is registered on the specified computer.

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

eventID
Int32 Int32 Int32 Int32

事件的應用程式特定識別項。The application-specific identifier for the event.

category
Int16 Int16 Int16 Int16

與訊息相關的應用程式特定的子分類。The application-specific subcategory associated with the message.

例外狀況

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

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

-或--or- eventID 小於零或大於 MaxValueeventID is less than zero or greater than MaxValue.

-或--or- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

int myEventID = 20;
short myCategory = 10;

// Write an informational entry to the event log.
Console::WriteLine( "Write from first source " );
EventLog::WriteEntry( "FirstSource", "Writing warning to event log.",
   EventLogEntryType::Information, myEventID, myCategory );
int myEventID = 20;
short myCategory = 10;
// Write an informational entry to the event log.
Console.WriteLine("Write from first source ");
EventLog.WriteEntry("FirstSource", "Writing warning to event log.",
                     EventLogEntryType.Information, myEventID, myCategory);
Dim myEventID As Integer = 10
Dim myCategory As Short = 20
' Write an informational entry to the event log.
Console.WriteLine("Write from first source ")
EventLog.WriteEntry("FirstSource", "Writing warning to event log.", _
                           EventLogEntryType.Information, myEventID, myCategory)

備註

使用此方法,將應用程式定義category的專案寫入事件記錄檔中,使用已註冊為適當記錄檔之事件來源的來源。Use this method to write an entry with an application-defined category to the event log, using a source that is already registered as an event source for the appropriate log. 事件檢視器使用類別來篩選事件來源所寫入的事件。The Event Viewer uses the category to filter events written by an event source. 事件檢視器可以將類別顯示為數值,也可以使用類別做為資源識別碼來顯示當地語系化的類別字串。The Event Viewer can display the category as a numeric value, or it can use the category as a resource identifier to display a localized category string.

注意

category參數應該是正數值。The category parameter should be a positive value. 負值類別目錄值會在事件檢視器中顯示為互補的正數。Negative category values appear as a complementary positive number in the Event Viewer. 例如,-10 會顯示為65526,並以-1 做為65535。For example, a -10 appears as 65,526, a -1 as 65,535.

若要在事件檢視器中顯示當地語系化的分類字串,您必須使用以類別資源檔設定的category事件來源,並將設為分類資源檔中的資源識別碼。To display localized category strings in the Event Viewer, you must use an event source configured with a category resource file, and set the category to a resource identifier in the category resource file. 如果事件來源沒有已設定的分類資源檔,或指定category的並未為分類資源檔中的字串編制索引,則事件檢視器會顯示該專案的數值類別目錄值。If the event source does not have a configured category resource file, or the specified category does not index a string in the category resource file, then the Event Viewer displays the numeric category value for that entry. EventLogInstaller 使用EventSourceCreationData或類別,設定類別資源檔,以及資源檔中的類別目錄字串數目。Configure the category resource file, along with the number of category strings in the resource file, using the EventLogInstaller or the EventSourceCreationData class.

除了類別以外,您還可以指定要寫入事件記錄檔之事件的事件識別碼。In addition to the category, you can specify an event identifier for the event being written to the event log. 事件識別碼和事件來源,可唯一識別事件。Event identifiers, along with the event source, uniquely identify an event. 每個應用程式都可以定義自己的編號事件和它們所對應的描述字串。Each application can define its own numbered events and the description strings to which they map. 事件檢視器會顯示這些字串值,以協助使用者瞭解發生了什麼錯誤,並建議採取哪些動作。Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

最後,您可以指定EventLogEntryType將事件寫入事件記錄檔的。Finally, you can specify an EventLogEntryType for the event being written to the event log. type是以記錄檔的事件檢視器中類型資料行內的圖示和文字來表示。The type is indicated by an icon and text in the Type column in the Event Viewer for a log. 此參數會指出事件種類為錯誤、警告、資訊、成功 audit 或失敗 audit。This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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

另請參閱

WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[]) WriteEntry(String, EventLogEntryType, Int32, Int16, Byte[])

將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔,並將二進位資料附加到訊息。Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log, and appends binary data to the message.

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID, short category, cli::array <System::Byte> ^ rawData);
public void WriteEntry (string message, System.Diagnostics.EventLogEntryType type, int eventID, short category, byte[] rawData);
member this.WriteEntry : string * System.Diagnostics.EventLogEntryType * int * int16 * byte[] -> unit
Public Sub WriteEntry (message As String, type As EventLogEntryType, eventID As Integer, category As Short, rawData As Byte())

參數

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

eventID
Int32 Int32 Int32 Int32

事件的應用程式特定識別項。The application-specific identifier for the event.

category
Int16 Int16 Int16 Int16

與訊息相關的應用程式特定的子分類。The application-specific subcategory associated with the message.

rawData
Byte[]

保存與項目相關的二進位資料的位元組陣列。An array of bytes that holds the binary data associated with the 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- eventID 小於零或大於 MaxValueeventID is less than zero or greater than MaxValue.

-或--or- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

// Create the source, if it does not already exist.
String^ myLogName = "myNewLog";
if (  !EventLog::SourceExists( "MySource" ) )
{
   EventLog::CreateEventSource( "MySource", myLogName );
   Console::WriteLine( "Creating EventSource" );
}
else
   myLogName = EventLog::LogNameFromSourceName( "MySource", "." );

// Create an EventLog and assign source.
EventLog^ myEventLog = gcnew EventLog;
myEventLog->Source = "MySource";
myEventLog->Log = myLogName;

// Set the 'description' for the event.
String^ myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType::Warning;
int myApplicatinEventId = 1100;
short myApplicatinCategoryId = 1;

// Set the 'data' for the event.
array<Byte>^ myRawData = gcnew array<Byte>(4);
for ( int i = 0; i < 4; i++ )
{
   myRawData[ i ] = 1;
}
Console::WriteLine( "Writing to EventLog.. " );
myEventLog->WriteEntry( myMessage, myEventLogEntryType, myApplicatinEventId, myApplicatinCategoryId, myRawData );
// Create the source, if it does not already exist.
string myLogName = "myNewLog";
if(!EventLog.SourceExists("MySource"))
{
   // An event log source should not be created and immediately used.
   // There is a latency time to enable the source, it should be created
   // prior to executing the application that uses the source.
   // Execute this sample a second time to use the new source.
   EventLog.CreateEventSource("MySource", myLogName);
   Console.WriteLine("Created EventSource");
   Console.WriteLine("Exiting, execute the application a second time to use the source.");
   return;
}
else
   myLogName = EventLog.LogNameFromSourceName("MySource",".");
// Create an EventLog and assign source.
EventLog myEventLog = new EventLog();
myEventLog.Source = "MySource";
myEventLog.Log = myLogName;

// Set the 'description' for the event.
string myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType.Warning;
int myApplicatinEventId = 1100;
short myApplicatinCategoryId = 1;

// Set the 'data' for the event.
byte[] myRawData = new byte[4];
for(int i=0;i<4;i++)
{
   myRawData[i]=1;
}
// Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ");
myEventLog.WriteEntry(myMessage,myEventLogEntryType, 
   myApplicatinEventId, myApplicatinCategoryId, myRawData);
' Create the source, if it does not already exist.
dim myLogName as string = "myNewLog"
If Not EventLog.SourceExists("MySource") Then
   EventLog.CreateEventSource("MySource", myLogName)
   Console.WriteLine("Creating EventSource")
else
   myLogName = EventLog.LogNameFromSourceName("MySource",".")
End If

' Create an EventLog and assign source.
Dim myEventLog As New EventLog()
myEventLog.Source = "MySource"
myEventLog.Log = myLogName

' Set the 'description' for the event.
Dim myMessage As String = "This is my event."
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Warning
Dim myApplicatinEventId As Integer = 1100
Dim myApplicatinCategoryId As Short = 1

' Set the 'data' for the event.
Dim myRawData(3) As Byte
Dim i As Integer
For i = 0 To 3
   myRawData(i) = 1
Next i
' Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ")
myEventLog.WriteEntry(myMessage, myEventLogEntryType, myApplicatinEventId, _
                     myApplicatinCategoryId, myRawData)

備註

使用此多載,將應用程式定義的事件特定資料寫入事件記錄檔。Use this overload to write application-defined event-specific data to the event log. 事件檢視器不會解讀此資料;它只會以結合的十六進位和文字格式顯示原始資料。The Event Viewer does not interpret this data; it displays raw data only in a combined hexadecimal and text format. 謹慎使用事件特定的資料,只有在您確定對問題的人員來說很有用時,才需要這麼做。Use event-specific data sparingly, including it only if you are sure it will be useful to someone debugging the problem. 您也可以使用事件特定的資料來儲存應用程式可以獨立處理事件檢視器的資訊。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 logfile and creates reports that include information from the event-specific data.

除了二進位資料以外,您還可以指定應用程式定義的分類和應用程式定義的事件識別碼。In addition to the binary data, you can specify an application-defined category and an application-defined event identifier. 事件檢視器使用類別來篩選事件來源所寫入的事件。The Event Viewer uses the category to filter events written by an event source. 事件檢視器可以將類別顯示為數值,也可以使用類別做為資源識別碼來顯示當地語系化的類別字串。The Event Viewer can display the category as a numeric value, or it can use the category as a resource identifier to display a localized category string.

注意

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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.

注意

category參數應該是正數值。The category parameter should be a positive value. 負值類別目錄值會在事件檢視器中顯示為互補的正數。Negative category values appear as a complementary positive number in the Event Viewer. 例如,-10 會顯示為65526,並以-1 做為65535。For example, a -10 appears as 65,526, a -1 as 65,535.

若要在事件檢視器中顯示當地語系化的分類字串,您必須使用以類別資源檔設定的category事件來源,並將設為分類資源檔中的資源識別碼。To display localized category strings in the Event Viewer, you must use an event source configured with a category resource file, and set the category to a resource identifier in the category resource file. 如果事件來源沒有已設定的分類資源檔,或指定category的並未為分類資源檔中的字串編制索引,則事件檢視器會顯示該專案的數值類別目錄值。If the event source does not have a configured category resource file, or the specified category does not index a string in the category resource file, then the Event Viewer displays the numeric category value for that entry. EventLogInstaller 使用EventSourceCreationData或類別,設定類別資源檔,以及資源檔中的類別目錄字串數目。Configure the category resource file, along with the number of category strings in the resource file, using the EventLogInstaller or the EventSourceCreationData class.

事件識別碼和事件來源,可唯一識別事件。Event identifiers, along with the event source, uniquely identify an event. 每個應用程式都可以定義自己的編號事件和它們所對應的描述字串。Each application can define its own numbered events and the description strings to which they map. 事件檢視器會顯示這些字串值,以協助使用者瞭解發生了什麼錯誤,並建議採取哪些動作。Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

最後,您可以指定EventLogEntryType將事件寫入事件記錄檔的。Finally, you can specify an EventLogEntryType for the event being written to the event log. type是以記錄檔的事件檢視器中類型資料行內的圖示和文字來表示。The type is indicated by an icon and text in the Type column in the Event Viewer for a log. 此參數會指出事件種類為錯誤、警告、資訊、成功 audit 或失敗 audit。This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

您必須先EventLogSource元件上設定屬性,才可以將專案寫入記錄檔。You must set the Source property on your EventLog component before you can write entries to the log. 在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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.

如果Source在這個EventLog實例的屬性中指定的來源未在您的元件所寫入的電腦上註冊, WriteEntry則會CreateEventSource呼叫並註冊來源。If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

注意

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

如果系統需要Source透過呼叫來WriteEntry註冊,而且尚未在您EventLogLog實例上設定屬性,則記錄檔會預設為應用程式記錄檔。If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

注意

以上所列的許多例外狀況都是由註冊Source過程中引發的錯誤所產生。Many exceptions listed above are generated by errors raised during the process of registering the Source.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 方法WriteEntry會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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.

注意

如果您將專案寫入遠端電腦,則如果遠端電腦未執行 .NET Framework,訊息的值(文字字串)可能就不是您預期的結果。If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

注意

message如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

安全性

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

另請參閱

WriteEntry(String, String, EventLogEntryType, Int32) WriteEntry(String, String, EventLogEntryType, Int32) WriteEntry(String, String, EventLogEntryType, Int32) WriteEntry(String, String, EventLogEntryType, Int32)

使用指定的登錄事件來源,將有指定訊息文字和應用程式定義的事件識別項的項目寫入事件記錄檔。Writes an entry with the given message text and application-defined event identifier to the event log, using the specified registered event source.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID);
public static void WriteEntry (string source, string message, System.Diagnostics.EventLogEntryType type, int eventID);
static member WriteEntry : string * string * System.Diagnostics.EventLogEntryType * int -> unit
Public Shared Sub WriteEntry (source As String, message As String, type As EventLogEntryType, eventID As Integer)

參數

source
String String String String

將應用程式註冊在指定電腦上的來源。The source by which the application is registered on the specified computer.

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

eventID
Int32 Int32 Int32 Int32

事件的應用程式特定識別項。The application-specific identifier for the event.

例外狀況

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

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

-或--or- eventID 小於零或大於 MaxValueeventID is less than zero or greater than MaxValue.

-或--or- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

// Create the source, if it does not already exist.
if (  !EventLog::SourceExists( "MySource" ) )
{
   EventLog::CreateEventSource( "MySource", "myNewLog" );
   Console::WriteLine( "Creating EventSource" );
}

// Set the 'description' for the event.
String^ myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType::Warning;
int myApplicationEventId = 100;

// Write the entry in the event log.
Console::WriteLine( "Writing to EventLog.. " );
EventLog::WriteEntry( "MySource", myMessage,
   myEventLogEntryType, myApplicationEventId );
// Create the source, if it does not already exist.
if(!EventLog.SourceExists("MySource"))
{
   // An event log source should not be created and immediately used.
   // There is a latency time to enable the source, it should be created
   // prior to executing the application that uses the source.
   // Execute this sample a second time to use the new source.
   EventLog.CreateEventSource("MySource", "myNewLog");
   Console.WriteLine("Creating EventSource");
   Console.WriteLine("Exiting, execute the application a second time to use the source.");
   // The source is created.  Exit the application to allow it to be registered.
   return;
}

// Set the 'description' for the event.
string myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType.Warning;
int myApplicationEventId = 100;

// Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ");
EventLog.WriteEntry("MySource",myMessage,
   myEventLogEntryType, myApplicationEventId);
' Create the source, if it does not already exist.
If Not EventLog.SourceExists("MySource") Then
   EventLog.CreateEventSource("MySource", "myNewLog")
   Console.WriteLine("Creating EventSource")
End If

' Set the 'description' for the event.
Dim myMessage As String = "This is my event."
Dim myEventLogEntryType As EventLogEntryType = EventLogEntryType.Warning
Dim myApplicationEventId As Integer = 100

' Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ")
EventLog.WriteEntry("MySource", myMessage, myEventLogEntryType, myApplicationEventId)

備註

使用此方法,將應用程式定義eventID的專案寫入事件記錄檔中,使用已註冊為適當記錄檔之事件來源的來源。Use this method to write an entry with an application-defined eventID to the event log, using a source already registered as an event source for the appropriate log. eventID和來源,可唯一識別事件。The eventID, along with the source, uniquely identify an event. 每個應用程式都可以定義自己的編號事件和它們所對應的描述字串。Each application can define its own numbered events and the description strings to which they map. 事件檢視器會向使用者顯示這些字串,以協助使用者瞭解發生了什麼問題,並建議採取哪些動作。Event viewers present these strings to the user to help the user understand what went wrong and suggest what actions to take.

注意

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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.

除了事件識別碼之外,的WriteEntry這個多載可讓您EventLogEntryType指定將事件寫入事件記錄檔的。In addition to the event identifier, this overload of WriteEntry lets you specify an EventLogEntryType for the event being written to the event log. type是以記錄檔的事件檢視器中類型資料行內的圖示和文字來表示。The type is indicated by an icon and text in the Type column in the Event Viewer for a log. 此參數會指出事件種類為錯誤、警告、資訊、成功 audit 或失敗 audit。This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

安全性

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

另請參閱

WriteEntry(String, EventLogEntryType, Int32, Int16) WriteEntry(String, EventLogEntryType, Int32, Int16) WriteEntry(String, EventLogEntryType, Int32, Int16) WriteEntry(String, EventLogEntryType, Int32, Int16)

將具有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔。Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log.

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID, short category);
public void WriteEntry (string message, System.Diagnostics.EventLogEntryType type, int eventID, short category);
member this.WriteEntry : string * System.Diagnostics.EventLogEntryType * int * int16 -> unit
Public Sub WriteEntry (message As String, type As EventLogEntryType, eventID As Integer, category As Short)

參數

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

eventID
Int32 Int32 Int32 Int32

事件的應用程式特定識別項。The application-specific identifier for the event.

category
Int16 Int16 Int16 Int16

與訊息相關的應用程式特定的子分類。The application-specific subcategory associated with the message.

例外狀況

還沒有設定 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- eventID 小於零或大於 MaxValueeventID is less than zero or greater than MaxValue.

-或--or- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

// Create an EventLog instance and assign its source.
EventLog^ myLog = gcnew EventLog;
myLog->Source = "ThirdSource";

// Write an informational entry to the event log.
Console::WriteLine( "Write from third source " );
myLog->WriteEntry( "Writing warning to event log.",
   EventLogEntryType::Warning, myEventID, myCategory );
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "ThirdSource";

// Write an informational entry to the event log.    
Console.WriteLine("Write from third source ");
myLog.WriteEntry("Writing warning to event log.",
                  EventLogEntryType.Warning, myEventID, myCategory);
' Create an EventLog instance and assign its source.
Dim myLog As New EventLog()
myLog.Source = "ThirdSource"

' Write an informational entry to the event log.
Console.WriteLine("Write from third source ")
myLog.WriteEntry("Writing warning to event log.", EventLogEntryType.Warning, _
                     myEventID, myCategory)

備註

使用此方法,將應用程式定義category的專案寫入事件記錄檔。Use this method to write an entry with an application-defined category to the event log. 事件檢視器使用類別來篩選事件來源所寫入的事件。The Event Viewer uses the category to filter events written by an event source. 事件檢視器可以將類別顯示為數值,也可以使用類別做為資源識別碼來顯示當地語系化的類別字串。The Event Viewer can display the category as a numeric value, or it can use the category as a resource identifier to display a localized category string.

注意

category參數應該是正數值。The category parameter should be a positive value. 負值類別目錄值會在事件檢視器中顯示為互補的正數。Negative category values appear as a complementary positive number in the Event Viewer. 例如,-10 會顯示為65526,並以-1 做為65535。For example, a -10 appears as 65,526, a -1 as 65,535.

注意

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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.

若要在事件檢視器中顯示當地語系化的分類字串,您必須使用以類別資源檔設定的category事件來源,並將設為分類資源檔中的資源識別碼。To display localized category strings in the Event Viewer, you must use an event source configured with a category resource file, and set the category to a resource identifier in the category resource file. 如果事件來源沒有已設定的分類資源檔,或指定category的並未為分類資源檔中的字串編制索引,則事件檢視器會顯示該專案的數值類別目錄值。If the event source does not have a configured category resource file, or the specified category does not index a string in the category resource file, then the Event Viewer displays the numeric category value for that entry. EventLogInstaller 使用EventSourceCreationData或類別,設定類別資源檔,以及資源檔中的類別目錄字串數目。Configure the category resource file, along with the number of category strings in the resource file, using the EventLogInstaller or the EventSourceCreationData class.

除了類別以外,您還可以指定要寫入事件記錄檔之事件的事件識別碼。In addition to the category, you can specify an event identifier for the event being written to the event log. 事件識別碼和事件來源,可唯一識別事件。Event identifiers, along with the event source, uniquely identify an event. 每個應用程式都可以定義自己的編號事件和它們所對應的描述字串。Each application can define its own numbered events and the description strings to which they map. 事件檢視器會顯示這些字串值,以協助使用者瞭解發生了什麼錯誤,並建議採取哪些動作。Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

最後,您可以指定EventLogEntryType將事件寫入事件記錄檔的。Finally, you can specify an EventLogEntryType for the event being written to the event log. type是以記錄檔的事件檢視器中類型資料行內的圖示和文字來表示。The type is indicated by an icon and text in the Type column in the Event Viewer for a log. 此參數會指出事件種類為錯誤、警告、資訊、成功 audit 或失敗 audit。This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

您必須先EventLogSource元件上設定屬性,才可以將專案寫入記錄檔。You must set the Source property on your EventLog component before you can write entries to the log. 在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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.

如果Source在這個EventLog實例的屬性中指定的來源未在您的元件所寫入的電腦上註冊, WriteEntry則會CreateEventSource呼叫並註冊來源。If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

注意

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

如果系統需要Source透過呼叫來WriteEntry註冊,而且尚未在您EventLogLog實例上設定屬性,則記錄檔會預設為應用程式記錄檔。If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

注意

以上所列的許多例外狀況都是由註冊Source過程中引發的錯誤所產生。Many exceptions listed above are generated by errors raised during the process of registering the Source.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 方法WriteEntry會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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.

注意

如果您將專案寫入遠端電腦,則如果遠端電腦未執行 .NET Framework,訊息的值(文字字串)可能就不是您預期的結果。If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

注意

message如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

安全性

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

另請參閱

WriteEntry(String, EventLogEntryType) WriteEntry(String, EventLogEntryType) WriteEntry(String, EventLogEntryType) WriteEntry(String, EventLogEntryType)

將具有指定訊息文字的錯誤、警告、資訊、成功稽核或失敗稽核項目寫入事件記錄檔。Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log.

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::EventLogEntryType type);
public void WriteEntry (string message, System.Diagnostics.EventLogEntryType type);
member this.WriteEntry : string * System.Diagnostics.EventLogEntryType -> unit
Public Sub WriteEntry (message As String, type As EventLogEntryType)

參數

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

例外狀況

還沒有設定 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- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

下列範例會將警告專案寫入至本機電腦上的事件記錄檔 "Mynewlog 清單"。The following example writes a warning entry to an event log, "MyNewLog", on the local computer.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog("MyNewLog");
   myLog->Source = "MyNewLogSource";
   
   // Write an informational entry to the event log.    
   myLog->WriteEntry( "Writing warning to event log.", EventLogEntryType::Warning );
}

using System;
using System.Diagnostics;
using System.Threading;
              
class MySample{

    public static void Main(){
    
                
        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog("MyNewLog");
        myLog.Source = "MyNewLogSource";
        
        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing warning to event log.", EventLogEntryType.Warning);
        
    }
}
      
Option Strict
Option Explicit

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog("MyNewLog")
        myLog.Source = "MyNewLogSource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing warning to event log.", EventLogEntryType.Warning)
    End Sub
End Class

備註

使用這個方法,將指定EventLogEntryType的專案寫入事件記錄檔。Use this method to write an entry of a specified EventLogEntryType to the event log. type是以記錄檔的事件檢視器中類型資料行內的圖示和文字來表示。The type is indicated by an icon and text in the Type column in the Event Viewer for a log.

注意

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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.

您必須先EventLogSource元件上設定屬性,才可以將專案寫入記錄檔。You must set the Source property on your EventLog component before you can write entries to the log. 在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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.

如果Source在這個EventLog實例的屬性中指定的來源未在您的元件所寫入的電腦上註冊, WriteEntry則會CreateEventSource呼叫並註冊來源。If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

注意

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

如果系統需要Source透過呼叫來WriteEntry註冊,而且尚未在您EventLogLog實例上設定屬性,則記錄檔會預設為應用程式記錄檔。If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

注意

以上所列的許多例外狀況都是由註冊Source過程中引發的錯誤所產生。Many exceptions listed above are generated by errors raised during the process of registering the Source.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 方法WriteEntry會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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.

注意

如果您將專案寫入遠端電腦,則如果遠端電腦未執行 .NET Framework,訊息的值(文字字串)可能就不是您預期的結果。If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

注意

message如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

安全性

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

另請參閱

WriteEntry(String, EventLogEntryType, Int32) WriteEntry(String, EventLogEntryType, Int32) WriteEntry(String, EventLogEntryType, Int32) WriteEntry(String, EventLogEntryType, Int32)

將有指定訊息文字和應用程式定義的事件識別項的項目寫入事件記錄檔。Writes an entry with the given message text and application-defined event identifier to the event log.

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::EventLogEntryType type, int eventID);
public void WriteEntry (string message, System.Diagnostics.EventLogEntryType type, int eventID);
member this.WriteEntry : string * System.Diagnostics.EventLogEntryType * int -> unit
Public Sub WriteEntry (message As String, type As EventLogEntryType, eventID As Integer)

參數

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

eventID
Int32 Int32 Int32 Int32

事件的應用程式特定識別項。The application-specific identifier for the event.

例外狀況

還沒有設定 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- eventID 小於零或大於 MaxValueeventID is less than zero or greater than MaxValue.

-或--or- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

// Create an EventLog instance and assign its source.
EventLog^ myLog = gcnew EventLog;
myLog->Source = "ThirdSource";

// Write an informational entry to the event log.
Console::WriteLine( "Write from third source " );
myLog->WriteEntry( "Writing warning to event log.",
   EventLogEntryType::Warning, myEventID, myCategory );
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "ThirdSource";

// Write an informational entry to the event log.    
Console.WriteLine("Write from third source ");
myLog.WriteEntry("Writing warning to event log.",
                  EventLogEntryType.Warning, myEventID, myCategory);
' Create an EventLog instance and assign its source.
Dim myLog As New EventLog()
myLog.Source = "ThirdSource"

' Write an informational entry to the event log.
Console.WriteLine("Write from third source ")
myLog.WriteEntry("Writing warning to event log.", EventLogEntryType.Warning, _
                     myEventID, myCategory)

備註

使用此方法,將應用程式定義eventID的專案寫入事件記錄檔。Use this method to write an entry with an application-defined eventID to the event log. 連同eventID來源唯一識別事件的。The eventID together with the source uniquely identify an event. 每個應用程式都可以定義自己的編號事件和它們所對應的描述字串。Each application can define its own numbered events and the description strings to which they map. 事件檢視器會顯示這些字串值,以協助使用者瞭解發生了什麼錯誤,並建議採取哪些動作。Event viewers display these string values to help the user understand what went wrong and suggest what actions to take.

注意

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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.

除了事件識別碼之外,您還可以指定EventLogEntryType將事件寫入事件記錄檔的。In addition to the event identifier, you can specify an EventLogEntryType for the event being written to the event log. type是以記錄檔的事件檢視器中類型資料行內的圖示和文字來表示。The type is indicated by an icon and text in the Type column in the Event Viewer for a log. 此參數會指出事件種類為錯誤、警告、資訊、成功 audit 或失敗 audit。This parameter indicates whether the event type is error, warning, information, success audit, or failure audit.

您必須先EventLogSource元件上設定屬性,才可以將專案寫入記錄檔。You must set the Source property on your EventLog component before you can write entries to the log. 在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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.

如果Source在這個EventLog實例的屬性中指定的來源未在您的元件所寫入的電腦上註冊, WriteEntry則會CreateEventSource呼叫並註冊來源。If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

注意

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

如果系統需要Source透過呼叫來WriteEntry註冊,而且尚未在您EventLogLog實例上設定屬性,則記錄檔會預設為應用程式記錄檔。If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

注意

以上所列的許多例外狀況都是由註冊Source過程中引發的錯誤所產生。Many exceptions listed above are generated by errors raised during the process of registering the Source.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 方法WriteEntry會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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.

注意

如果您將專案寫入遠端電腦,則如果遠端電腦未執行 .NET Framework,訊息的值(文字字串)可能就不是您預期的結果。If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

注意

message如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

安全性

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

另請參閱

WriteEntry(String, String) WriteEntry(String, String) WriteEntry(String, String) WriteEntry(String, String)

使用指定的登錄事件來源,將具有指定訊息文字的資訊類型項目寫入事件記錄檔。Writes an information type entry with the given message text to the event log, using the specified registered event source.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message);
public static void WriteEntry (string source, string message);
static member WriteEntry : string * string -> unit
Public Shared Sub WriteEntry (source As String, message As String)

參數

source
String String String String

將應用程式註冊在指定電腦上的來源。The source by which the application is registered on the specified computer.

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

例外狀況

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

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

-或--or- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

下列範例會建立來源MySource (如果尚未存在),並將專案寫入事件記錄MyNewLog檔。The following example creates the source MySource if it does not already exist, and writes an entry to the event log MyNewLog.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Create the source, if it does not already exist.
   if (  !EventLog::SourceExists( "MySource" ) )
   {
      EventLog::CreateEventSource( "MySource", "myNewLog" );
      Console::WriteLine( "CreatingEventSource" );
   }

   
   // Write an informational entry to the event log.    
   EventLog::WriteEntry( "MySource", "Writing to event log." );
}

using System;
using System.Diagnostics;
using System.Threading;
              
class MySample{

    public static void Main(){
    
        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource"))
        {
            // An event log source should not be created and immediately used.
            // There is a latency time to enable the source, it should be created
            // prior to executing the application that uses the source.
            // Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "myNewLog");
            Console.WriteLine("CreatingEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }
                
      
        // Write an informational entry to the event log.    
        EventLog.WriteEntry("MySource", "Writing to event log.");
        
        
    }
}

Option Strict
Option Explicit

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Create the source, if it does not already exist.
        If Not EventLog.SourceExists("MySource") Then
            EventLog.CreateEventSource("MySource", "myNewLog")
            Console.WriteLine("CreatingEventSource")
        End If
        
        
        ' Write an informational entry to the event log.    
        EventLog.WriteEntry("MySource", "Writing to event log.")
    End Sub
End Class

備註

使用此方法,將資訊專案寫入事件記錄檔,使用已註冊為適當記錄檔之事件來源的來源。Use this method to write an information entry to the event log, using a source that is already registered as an event source for the appropriate log. 如果您想要指定任何其他EventLogEntryType,請使用不同的WriteEntry多載。If you want to specify any other EventLogEntryType, use a different overload of WriteEntry.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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

另請參閱

WriteEntry(String) WriteEntry(String) WriteEntry(String) WriteEntry(String)

將具有指定訊息文字的資訊類型項目寫入事件記錄檔。Writes an information type entry, with the given message text, to the event log.

public:
 void WriteEntry(System::String ^ message);
public void WriteEntry (string message);
member this.WriteEntry : string -> unit
Public Sub WriteEntry (message As String)

參數

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

例外狀況

還沒有設定 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- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

下列範例會建立來源MySource (如果尚未存在),並將專案寫入事件記錄MyNewLog檔。The following example creates the source MySource if it does not already exist, and writes an entry to the event log MyNewLog.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Create the source, if it does not already exist.
   if (  !EventLog::SourceExists( "MySource" ) )
   {
      //An event log source should not be created and immediately used.
      //There is a latency time to enable the source, it should be created
      //prior to executing the application that uses the source.
      //Execute this sample a second time to use the new source.
      EventLog::CreateEventSource( "MySource", "MyNewLog" );
      Console::WriteLine( "CreatingEventSource" );
      // The source is created.  Exit the application to allow it to be registered.
      return 0;
   }

   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog;
   myLog->Source = "MySource";
   
   // Write an informational entry to the event log.    
   myLog->WriteEntry( "Writing to event log." );
}

using System;
using System.Diagnostics;
using System.Threading;
              
class MySample{

    public static void Main(){
    
        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource"))
        {
             //An event log source should not be created and immediately used.
             //There is a latency time to enable the source, it should be created
             //prior to executing the application that uses the source.
             //Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatedEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }
                
        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";
        
        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");
        
    }
}
   
Option Explicit
Option Strict

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        
        If Not EventLog.SourceExists("MySource") Then
            ' Create the source, if it does not already exist.
            ' An event log source should not be created and immediately used.
            ' There is a latency time to enable the source, it should be created
            ' prior to executing the application that uses the source.
            ' Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog")
            Console.WriteLine("CreatingEventSource")
            'The source is created.  Exit the application to allow it to be registered.
            Return
        End If
        
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog()
        myLog.Source = "MySource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.")
    End Sub
End Class

備註

使用此方法,將資訊專案寫入與這個EventLog實例相關聯的事件記錄檔。Use this method to write an information entry to the event log associated with this EventLog instance. 如果您想要指定任何其他EventLogEntryType,請使用不同的WriteEntry多載。If you want to specify any other EventLogEntryType, use a different overload of WriteEntry.

注意

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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.

您必須先EventLogSource元件上設定屬性,才可以將專案寫入記錄檔。You must set the Source property on your EventLog component before you can write entries to the log. 在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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.

如果Source在這個EventLog實例的屬性中指定的來源未在您的元件所寫入的電腦上註冊, WriteEntry則會CreateEventSource呼叫並註冊來源。If the source specified in the Source property of this EventLog instance is not registered on the computer that your component is writing to, WriteEntry calls CreateEventSource and registers the source.

注意

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

如果系統需要Source透過呼叫來WriteEntry註冊,而且尚未在您EventLogLog實例上設定屬性,則記錄檔會預設為應用程式記錄檔。If the system needs to register the Source through a call to WriteEntry and the Log property has not been set on your EventLog instance, the log defaults to the Application log.

注意

以上所列的許多例外狀況都是由註冊Source過程中引發的錯誤所產生。Many of the exceptions listed above are generated by errors raised during the process of registering the Source.

來源必須設定為寫入當地語系化的專案或撰寫直接字串。The source must be configured either for writing localized entries or for writing direct strings. 方法WriteEntry會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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.

注意

如果您將專案寫入遠端電腦,則如果遠端電腦未執行 .NET Framework,訊息的值(文字字串)可能就不是您預期的結果。If you write an entry to a remote computer, the value of the message (the text string) might not be what you expect if the remote computer is not running the .NET Framework.

注意

message如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

安全性

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

另請參閱

WriteEntry(String, String, EventLogEntryType) WriteEntry(String, String, EventLogEntryType) WriteEntry(String, String, EventLogEntryType) WriteEntry(String, String, EventLogEntryType)

用指定的登錄事件來源,將有指定訊息文字的錯誤、警告、資訊、成功稽核或失敗稽核項目寫入事件記錄檔。Writes an error, warning, information, success audit, or failure audit entry with the given message text to the event log, using the specified registered event source.

public:
 static void WriteEntry(System::String ^ source, System::String ^ message, System::Diagnostics::EventLogEntryType type);
public static void WriteEntry (string source, string message, System.Diagnostics.EventLogEntryType type);
static member WriteEntry : string * string * System.Diagnostics.EventLogEntryType -> unit
Public Shared Sub WriteEntry (source As String, message As String, type As EventLogEntryType)

參數

source
String String String String

將應用程式註冊在指定電腦上的來源。The source by which the application is registered on the specified computer.

message
String String String String

要寫入事件記錄檔的字串。The string to write to the event log.

例外狀況

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

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

-或--or- 訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。The message string is longer than 31,839 bytes (32,766 bytes on Windows operating systems before Windows Vista).

-或--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.

範例

下列範例會將警告專案寫入至本機電腦上的事件記錄檔 "Mynewlog 清單"。The following example writes a warning entry to an event log, "MyNewLog", on the local computer.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   
   // Write an informational entry to the event log.    
   EventLog::WriteEntry( "MySource", "Writing warning to event log.", EventLogEntryType::Warning );
}

using System;
using System.Diagnostics;
using System.Threading;
              
class MySample{

    public static void Main(){
                    
      
        // Write an informational entry to the event log.    
        EventLog.WriteEntry("MySource", 
            "Writing warning to event log.", 
            EventLogEntryType.Warning);
        
    }
}
      
Option Explicit
Option Strict

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Write an informational entry to the event log.    
        EventLog.WriteEntry("MySource", "Writing warning to event log.", _
            EventLogEntryType.Warning)
    End Sub
End Class

備註

使用此方法,將指定EventLogEntryType的專案寫入事件記錄檔中,使用已註冊為適當記錄檔之事件來源的來源。Use this method to write an entry of a specified EventLogEntryType to the event log, using a source already registered as an event source for the appropriate log. type是以記錄檔的事件檢視器中類型資料行內的圖示和文字來表示。The type is indicated by an icon and text in the Type column in the Event Viewer for a log.

注意

字串不能包含%n,其中 n 是整數值(例如% 1),因為事件檢視器將它視為插入字串。 messageThe 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.

在寫入具有來源的第一個專案之前,您必須先建立和設定事件來源。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會將指定的字串直接寫入事件記錄檔,而不會使用可當地語系化的訊息資源檔。The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. WriteEvent使用方法,利用當地語系化的訊息資源檔來寫入事件。Use the WriteEvent method to write events using a localized message resource file.

如果您的應用程式使用資源識別碼和字串值來寫入專案,您必須註冊兩個不同的來源。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如果參數包含 nul 字元,則事件記錄檔中的訊息會在 NUL 字元結束。If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

安全性

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

另請參閱

適用於