EventLog.WriteEntry 方法

定義

將項目寫入事件記錄檔中。

多載

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

將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔 (使用指定的登錄事件來源),並將二進位資料附加到訊息。

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

使用指定的登錄事件來源,將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔。 category 可以讓事件檢視器用來篩選記錄檔中的事件。

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

將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔,並將二進位資料附加到訊息。

WriteEntry(String, String, EventLogEntryType, Int32)

使用指定的登錄事件來源,將有指定訊息文字和應用程式定義的事件識別項的項目寫入事件記錄檔。

WriteEntry(String, EventLogEntryType, Int32, Int16)

將具有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔。

WriteEntry(String, EventLogEntryType)

將具有指定訊息文字的錯誤、警告、資訊、成功稽核或失敗稽核項目寫入事件記錄檔。

WriteEntry(String, EventLogEntryType, Int32)

將有指定訊息文字和應用程式定義的事件識別項的項目寫入事件記錄檔。

WriteEntry(String, String)

使用指定的登錄事件來源,將具有指定訊息文字的資訊類型項目寫入事件記錄檔。

WriteEntry(String)

將具有指定訊息文字的資訊類型項目寫入事件記錄檔。

WriteEntry(String, String, EventLogEntryType)

用指定的登錄事件來源,將有指定訊息文字的錯誤、警告、資訊、成功稽核或失敗稽核項目寫入事件記錄檔。

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

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

將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔 (使用指定的登錄事件來源),並將二進位資料附加到訊息。

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

將應用程式註冊在指定電腦上的來源。

message
String

要寫入事件記錄檔的字串。

type
EventLogEntryType

其中一個 EventLogEntryType 值。

eventID
Int32

事件的應用程式特定識別項。

category
Int16

與訊息相關的應用程式特定的子分類。

rawData
Byte[]

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

例外狀況

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

-或-

source 值為 null

-或-

eventID 小於零或大於 UInt16.MaxValue

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

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

備註

使用這個方法,將應用程式定義的事件特定數據寫入事件記錄檔,並使用已經註冊為適當記錄的事件來源。 事件檢視器 不會解譯此數據;它只會以合併的十六進位和文字格式顯示原始數據。 謹慎使用事件特定的數據;只有在確定它很有用時,才包含它。 您也可以使用事件特定資料來儲存應用程式可以獨立於 事件檢視器 處理的資訊。 例如,您可以特別為您的事件撰寫查看器,或撰寫程式來掃描記錄檔,並建立包含事件特定數據資訊的報表。

除了二進位數據之外,您還可以指定應用程式定義的類別和應用程式定義的事件識別碼。 事件檢視器 會使用 類別來篩選事件來源所寫入的事件。 事件檢視器 可以將類別顯示為數值,也可以使用類別做為資源標識符來顯示本地化的類別字串。

注意

參數 category 應該是正值。 負數類別值會顯示為 事件檢視器 中的互補正數。 例如,-10 會顯示為65,526,也就是 -1 顯示為65,535。

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

事件標識碼與事件來源一起,可唯一識別事件。 每個應用程式都可以定義自己的編號事件及其對應的描述字串。 事件查看器會顯示這些字串值,以協助使用者了解發生錯誤的內容,並建議採取哪些動作。

最後,您可以寫入事件記錄檔的事件指定 EventLogEntryType 。 會type以記錄檔 事件檢視器 中 [類型] 資料列中的圖示和文字表示。 此參數指出事件類型為錯誤、警告、資訊、成功稽核或失敗稽核。

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

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

另請參閱

適用於

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

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

使用指定的登錄事件來源,將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔。 category 可以讓事件檢視器用來篩選記錄檔中的事件。

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

將應用程式註冊在指定電腦上的來源。

message
String

要寫入事件記錄檔的字串。

type
EventLogEntryType

其中一個 EventLogEntryType 值。

eventID
Int32

事件的應用程式特定識別項。

category
Int16

與訊息相關的應用程式特定的子分類。

例外狀況

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

-或-

source 值為 null

-或-

eventID 小於零或大於 UInt16.MaxValue

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

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 專案寫入事件記錄檔。 事件檢視器 會使用 類別來篩選事件來源所寫入的事件。 事件檢視器 可以將類別顯示為數值,或者可以使用類別作為資源標識符來顯示本地化的類別字串。

注意

參數 category 應該是正值。 負數類別值會顯示為 事件檢視器 中的互補正數。 例如,-10 會顯示為65,526,-1為65,535。

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

除了類別之外,您還可以為寫入事件記錄檔的事件指定事件標識符。 事件標識碼以及事件來源會唯一識別事件。 每個應用程式都可以定義自己的編號事件及其對應的描述字串。 事件查看器會顯示這些字串值,以協助使用者了解發生錯誤的內容,並建議採取哪些動作。

最後,您可以寫入事件記錄檔的事件指定 EventLogEntryType 。 會type以記錄檔 事件檢視器 中 [類型] 資料列中的圖示和文字表示。 此參數指出事件類型為錯誤、警告、資訊、成功稽核或失敗稽核。

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

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

另請參閱

適用於

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

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

將有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔,並將二進位資料附加到訊息。

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

要寫入事件記錄檔的字串。

type
EventLogEntryType

其中一個 EventLogEntryType 值。

eventID
Int32

事件的應用程式特定識別項。

category
Int16

與訊息相關的應用程式特定的子分類。

rawData
Byte[]

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

例外狀況

還沒有設定 SourceEventLog 屬性。

-或-

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

-或-

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

-或-

eventID 小於零或大於 UInt16.MaxValue

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

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

備註

使用此多載將應用程式定義的事件特定數據寫入事件記錄檔。 事件檢視器 不會解譯此數據;它只會以合併的十六進位和文字格式顯示原始數據。 請謹慎使用事件特定的數據,只有在您確定對問題進行偵錯時,才會包含該數據。 您也可以使用事件特定資料來儲存應用程式可以獨立於 事件檢視器 處理的資訊。 例如,您可以特別為您的事件撰寫查看器,或撰寫程式來掃描記錄檔,並建立包含事件特定數據資訊的報表。

除了二進位數據之外,您還可以指定應用程式定義的類別和應用程式定義的事件識別碼。 事件檢視器 會使用 類別來篩選事件來源所寫入的事件。 事件檢視器 可以將類別顯示為數值,或者可以使用類別作為資源標識符來顯示本地化的類別字串。

注意

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

注意

參數 category 應該是正值。 負數類別值會顯示為 事件檢視器 中的互補正數。 例如,-10 會顯示為65,526,-1為65,535。

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

事件標識碼以及事件來源會唯一識別事件。 每個應用程式都可以定義自己的編號事件及其對應的描述字串。 事件查看器會顯示這些字串值,以協助使用者了解發生錯誤的內容,並建議採取哪些動作。

最後,您可以寫入事件記錄檔的事件指定 EventLogEntryType 。 會type以記錄檔 事件檢視器 中 [類型] 資料列中的圖示和文字表示。 此參數指出事件類型為錯誤、警告、資訊、成功稽核或失敗稽核。

您必須先在元件上EventLog設定 Source 屬性,才能將專案寫入記錄檔。 您必須先建立並設定事件來源,才能使用來源撰寫第一個專案。

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

如果這個EventLog實例的 屬性中指定的Source來源未在元件寫入的計算機上註冊,WriteEntry請呼叫CreateEventSource並註冊來源。

注意

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

如果系統需要透過呼叫 WriteEntry 來註冊 Source ,而且Log實例上EventLog尚未設定 屬性,則記錄檔預設為應用程式記錄檔。

注意

上述許多例外狀況是由註冊 Source程式期間引發的錯誤所產生。

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

如果您將專案寫入遠端電腦,當遠端電腦未執行 .NET Framework 時, (文字字串) 訊息的值可能不是您預期的值。

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

另請參閱

適用於

WriteEntry(String, String, EventLogEntryType, Int32)

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

使用指定的登錄事件來源,將有指定訊息文字和應用程式定義的事件識別項的項目寫入事件記錄檔。

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

將應用程式註冊在指定電腦上的來源。

message
String

要寫入事件記錄檔的字串。

type
EventLogEntryType

其中一個 EventLogEntryType 值。

eventID
Int32

事件的應用程式特定識別項。

例外狀況

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

-或-

source 值為 null

-或-

eventID 小於零或大於 UInt16.MaxValue

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

// 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 專案寫入事件記錄檔,並使用已經註冊為適當記錄的事件來源。 與 eventID來源一起,可唯一識別事件。 每個應用程式都可以定義自己的編號事件及其對應的描述字串。 事件查看器會將這些字串呈現給使用者,以協助使用者了解發生錯誤的內容,並建議採取哪些動作。

注意

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

除了事件標識碼之外,這個的多載 WriteEntry 可讓您指定 EventLogEntryType 寫入事件記錄檔的事件。 會type以記錄檔 事件檢視器 中 [類型] 資料列中的圖示和文字表示。 此參數指出事件類型為錯誤、警告、資訊、成功稽核或失敗稽核。

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

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

另請參閱

適用於

WriteEntry(String, EventLogEntryType, Int32, Int16)

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

將具有指定訊息文字、應用程式定義的事件識別項和應用程式定義的分類的項目寫入事件記錄檔。

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

要寫入事件記錄檔的字串。

type
EventLogEntryType

其中一個 EventLogEntryType 值。

eventID
Int32

事件的應用程式特定識別項。

category
Int16

與訊息相關的應用程式特定的子分類。

例外狀況

還沒有設定 SourceEventLog 屬性。

-或-

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

-或-

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

-或-

eventID 小於零或大於 UInt16.MaxValue

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

// 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 專案寫入事件記錄檔。 事件檢視器 會使用 類別來篩選事件來源所寫入的事件。 事件檢視器 可以將類別顯示為數值,也可以使用類別做為資源標識符來顯示本地化的類別字串。

注意

參數 category 應該是正值。 負數類別值會顯示為 事件檢視器 中的互補正數。 例如,-10 會顯示為65,526,-1為65,535。

注意

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

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

除了類別之外,您還可以為寫入事件記錄檔的事件指定事件標識符。 事件標識碼以及事件來源會唯一識別事件。 每個應用程式都可以定義自己的編號事件及其對應的描述字串。 事件查看器會顯示這些字串值,以協助使用者了解發生錯誤的內容,並建議採取哪些動作。

最後,您可以寫入事件記錄檔的事件指定 EventLogEntryType 。 會type以記錄檔 事件檢視器 中 [類型] 資料列中的圖示和文字表示。 此參數指出事件類型為錯誤、警告、資訊、成功稽核或失敗稽核。

您必須先在元件上EventLog設定 Source 屬性,才能將專案寫入記錄檔。 您必須先建立並設定事件來源,才能使用來源撰寫第一個專案。

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

如果這個EventLog實例的 屬性中指定的Source來源未在元件寫入的計算機上註冊,WriteEntry請呼叫CreateEventSource並註冊來源。

注意

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

如果系統需要透過呼叫 WriteEntry 來註冊 Source ,而且Log實例上EventLog尚未設定 屬性,則記錄檔預設為應用程式記錄檔。

注意

上述許多例外狀況是由註冊 Source程式期間引發的錯誤所產生。

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

如果您將專案寫入遠端電腦,如果遠端電腦未執行 .NET Framework,則訊息的值 (文字字串) 可能不是您預期的值。

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

另請參閱

適用於

WriteEntry(String, EventLogEntryType)

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

將具有指定訊息文字的錯誤、警告、資訊、成功稽核或失敗稽核項目寫入事件記錄檔。

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

要寫入事件記錄檔的字串。

type
EventLogEntryType

其中一個 EventLogEntryType 值。

例外狀況

還沒有設定 SourceEventLog 屬性。

-或-

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

-或-

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

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

下列範例會將警告專案寫入本機電腦上的事件記錄檔 「MyNewLog」。。

#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 MySample1
{

    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 專案寫入事件記錄檔。 會type以記錄檔 事件檢視器 中 [類型] 資料列中的圖示和文字表示。

注意

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

您必須先在元件上EventLog設定 Source 屬性,才能將專案寫入記錄檔。 您必須先建立並設定事件來源,才能使用來源撰寫第一個專案。

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

如果這個EventLog實例的 屬性中指定的Source來源未在元件寫入的計算機上註冊,WriteEntry請呼叫CreateEventSource並註冊來源。

注意

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

如果系統需要透過呼叫 WriteEntry 來註冊 Source ,而且Log實例上EventLog尚未設定 屬性,則記錄檔預設為應用程式記錄檔。

注意

上述許多例外狀況是由註冊 Source程式期間引發的錯誤所產生。

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

如果您將專案寫入遠端電腦,如果遠端電腦未執行 .NET Framework,則訊息的值 (文字字串) 可能不是您預期的值。

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

另請參閱

適用於

WriteEntry(String, EventLogEntryType, Int32)

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

將有指定訊息文字和應用程式定義的事件識別項的項目寫入事件記錄檔。

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

要寫入事件記錄檔的字串。

type
EventLogEntryType

其中一個 EventLogEntryType 值。

eventID
Int32

事件的應用程式特定識別項。

例外狀況

還沒有設定 SourceEventLog 屬性。

-或-

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

-或-

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

-或-

eventID 小於零或大於 UInt16.MaxValue

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

// 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 專案寫入事件記錄檔。 與 eventID 來源一起唯一識別事件。 每個應用程式都可以定義自己的編號事件及其對應的描述字串。 事件查看器會顯示這些字串值,以協助使用者了解發生錯誤的內容,並建議採取哪些動作。

注意

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

除了事件識別碼之外,您還可以為寫入事件記錄檔的事件指定 EventLogEntryType 。 會type以記錄檔 事件檢視器 中 [類型] 資料列中的圖示和文字表示。 此參數指出事件類型為錯誤、警告、資訊、成功稽核或失敗稽核。

您必須先在元件上EventLog設定 Source 屬性,才能將專案寫入記錄檔。 您必須先建立並設定事件來源,才能使用來源撰寫第一個專案。

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

如果這個EventLog實例的 屬性中指定的Source來源未在元件寫入的計算機上註冊,WriteEntry請呼叫CreateEventSource並註冊來源。

注意

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

如果系統需要透過呼叫 WriteEntry 來註冊 Source ,而且Log實例上EventLog尚未設定 屬性,則記錄檔預設為應用程式記錄檔。

注意

上述許多例外狀況是由註冊 Source程式期間引發的錯誤所產生。

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

如果您將專案寫入遠端電腦,當遠端電腦未執行 .NET Framework 時, (文字字串) 訊息的值可能不是您預期的值。

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

另請參閱

適用於

WriteEntry(String, String)

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

使用指定的登錄事件來源,將具有指定訊息文字的資訊類型項目寫入事件記錄檔。

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

將應用程式註冊在指定電腦上的來源。

message
String

要寫入事件記錄檔的字串。

例外狀況

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

-或-

source 值為 null

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

下列範例會在來源不存在時建立來源 MySource ,並將專案寫入事件記錄檔 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

備註

使用此方法可將資訊專案寫入事件記錄檔,並使用已經註冊為適當記錄的事件來源。 如果您想要指定任何其他 EventLogEntryType,請使用 不同的 多 WriteEntry載。

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

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

另請參閱

適用於

WriteEntry(String)

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

將具有指定訊息文字的資訊類型項目寫入事件記錄檔。

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

參數

message
String

要寫入事件記錄檔的字串。

例外狀況

還沒有設定 SourceEventLog 屬性。

-或-

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

-或-

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

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

下列範例會在來源不存在時建立來源 MySource ,並將專案寫入事件記錄檔 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 實例相關聯的事件記錄檔。 如果您想要指定任何其他 EventLogEntryType,請使用 不同的 多 WriteEntry載。

注意

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

您必須先在元件上EventLog設定 Source 屬性,才能將專案寫入記錄檔。 您必須先建立並設定事件來源,才能使用來源撰寫第一個專案。

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

如果這個EventLog實例的 屬性中指定的Source來源未在元件寫入的計算機上註冊,WriteEntry請呼叫CreateEventSource並註冊來源。

注意

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

如果系統需要透過呼叫 WriteEntry 來註冊 Source ,而且Log實例上EventLog尚未設定 屬性,則記錄檔預設為應用程式記錄檔。

注意

上述許多例外狀況都是在註冊 Source期間引發的錯誤所產生。

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

如果您將專案寫入遠端電腦,當遠端電腦未執行 .NET Framework 時, (文字字串) 訊息的值可能不是您預期的值。

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

另請參閱

適用於

WriteEntry(String, String, EventLogEntryType)

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

用指定的登錄事件來源,將有指定訊息文字的錯誤、警告、資訊、成功稽核或失敗稽核項目寫入事件記錄檔。

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

將應用程式註冊在指定電腦上的來源。

message
String

要寫入事件記錄檔的字串。

type
EventLogEntryType

其中一個 EventLogEntryType 值。

例外狀況

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

-或-

source 值為 null

-或-

訊息字串的長度超過 31,839 個位元組 (在 Windows Vista 之前的 Windows 作業系統中為 32,766 位元組)。

-或-

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

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

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

範例

下列範例會將警告專案寫入本機電腦上的事件記錄檔 「MyNewLog」。。

#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 MySample2
{

    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 專案寫入事件記錄檔,並使用已經註冊為適當記錄的事件來源。 會type以記錄檔 事件檢視器 中 [類型] 資料列中的圖示和文字表示。

注意

message字串不能包含 %n,其中 n 是整數值 (,例如%1) ,因為事件查看器會將它視為插入字串。 因為因特網通訊協定版本 6 (IPv6) 位址可以包含此字元序列,所以您無法記錄包含 IPv6 位址的事件訊息。

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

來源必須設定為撰寫本地化專案,或撰寫直接字串。 方法 WriteEntry 會將指定的字串直接寫入事件記錄檔;它不會使用可本地化的訊息資源檔。 使用方法, WriteEvent 使用本地化的訊息資源檔來寫入事件。

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

注意

message如果參數包含 NUL 字元,事件記錄檔中的訊息會在 NUL 字元終止。

另請參閱

適用於