.NET Framework Class Library EventLog..::.WriteEntry Method (String, EventLogEntryType, Int32, Int16, array<Byte>[]()[]) Updated: May 2010 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.
Namespace:
System.Diagnostics
Assembly:
System (in System.dll)

Syntax
Public Sub WriteEntry ( _
message As String, _
type As EventLogEntryType, _
eventID As Integer, _
category As Short, _
rawData As Byte() _
)
public void WriteEntry(
string message,
EventLogEntryType type,
int eventID,
short category,
byte[] rawData
)
public:
void WriteEntry(
String^ message,
EventLogEntryType type,
int eventID,
short category,
array<unsigned char>^ rawData
)
member WriteEntry :
message:string *
type:EventLogEntryType *
eventID:int *
category:int16 *
rawData:byte[] -> unit

Exceptions
| Exception | Condition |
|---|
| ArgumentException | The Source property of the EventLog has not been set. -or- 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 is less than zero or greater than UInt16..::.MaxValue. - or - The message string is longer than 32766 bytes. - or - The source name results in a registry key path longer than 254 characters. | | InvalidOperationException | The registry key for the event log could not be opened. | | InvalidEnumArgumentException |
type is not a valid EventLogEntryType. | | Win32Exception | The operating system reported an error when writing the event entry to the event log. A Windows error code is not available. |

Remarks
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. Note |
|---|
The message string cannot contain %n, where n is an integer value (for example, %1), because the event viewer treats it as an insertion string. Because an Internet Protocol, version 6 (IPv6) address can contain this character sequence, you cannot log an event message that contains an IPv6 address. |
Note |
|---|
The category parameter should be a positive value. Negative category values appear as a complementary positive number in the Event Viewer. For example, a –10 appears as 65,526, a –1 as 65,535. |
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. 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. 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. Finally, you can specify an EventLogEntryType for the event being written to the event log. The type is indicated by an icon and text in the Type column in the Event Viewer for a log. This parameter indicates whether the event type is error, warning, information, success audit, or failure audit. 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. 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. 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. 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. Note |
|---|
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. The WriteEntry method writes the given string directly to the event log; it does not use a localizable message resource file. 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. 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. 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. Note |
|---|
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. |
Note |
|---|
If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character. |

Examples
' 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)
// 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.
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 );

Version Information
.NET FrameworkSupported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileSupported in: 4, 3.5 SP1

.NET Framework Security

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

See Also

Change History
Date | History | Reason |
|---|
May 2010
| Added note about use of %n in message parameter. |
Content bug fix.
|
|
.NET Framework-Klassenbibliothek EventLog..::.WriteEntry-Methode (String, EventLogEntryType, Int32, Int16, array<Byte>[]()[]) Schreibt einen Eintrag mit dem angegebenen Meldungstext, dem von der Anwendung definierten Ereignisbezeichner und der Kategorie in das Ereignisprotokoll und fügt Binärdaten an die Meldung an.
Namespace:
System.Diagnostics
Assembly:
System (in System.dll)

Syntax
Public Sub WriteEntry ( _
message As String, _
type As EventLogEntryType, _
eventID As Integer, _
category As Short, _
rawData As Byte() _
)
public void WriteEntry(
string message,
EventLogEntryType type,
int eventID,
short category,
byte[] rawData
)
public:
void WriteEntry(
String^ message,
EventLogEntryType type,
int eventID,
short category,
array<unsigned char>^ rawData
)
member WriteEntry :
message:string *
type:EventLogEntryType *
eventID:int *
category:int16 *
rawData:byte[] -> unit

Ausnahmen
| Ausnahme | Bedingung |
|---|
| ArgumentException | Die Source-Eigenschaft von EventLog wurde nicht festgelegt. – oder – Die Methode hat versucht, eine neue Ereignisquelle zu registrieren, der Computername in MachineName ist jedoch ungültig. - oder - Die Quelle ist bereits für ein anderes Ereignisprotokoll registriert. - oder -
eventID ist kleiner als 0 oder größer als UInt16..::.MaxValue. - oder - Die Meldungszeichenfolge ist länger als 32.766 Bytes. - oder - Der Quellenname führt dazu, dass der Registrierungsschlüsselpfad länger als 254 Zeichen ist. | | InvalidOperationException | Der Registrierungsschlüssel für das Ereignisprotokoll konnte nicht geöffnet werden. | | InvalidEnumArgumentException |
type ist kein gültiger EventLogEntryType. | | Win32Exception | Das Betriebssystem hat beim Schreiben des Ereigniseintrags in das Ereignisprotokoll einen Fehler gemeldet. Es ist kein Windows-Fehlercode verfügbar. |

Hinweise
Schreiben Sie mithilfe dieser Überladung von der Anwendung definierte ereignisspezifische Daten in das Ereignisprotokoll. Die Ereignisanzeige interpretiert diese Daten nicht, sondern zeigt nur Rohdaten in einem kombinierten Hexadezimal- und Textformat an. Verwenden Sie ereignisspezifische Daten nur selten, und geben Sie diese nur an, wenn diese für die Fehlerbehebung nützlich sind. Sie können die ereignisspezifischen Daten auch zum Speichern von Informationen verwenden, die die Anwendung unabhängig von der Ereignisanzeige verarbeiten kann. Sie könnten z. B. einen auf die eigenen Ereignisse ausgerichteten Viewer oder ein Programm schreiben, das die Protokolldatei scannt und Berichte erstellt, die Informationen aus den ereignisspezifischen Daten enthalten. Zusätzlich zu den Binärdaten können Sie eine von der Anwendung definierte Kategorie und einen von der Anwendung definierten Ereignisbezeichner angeben. Die Ereignisanzeige filtert anhand der Kategorie die von einer Ereignisquelle geschriebenen Ereignisse. In der Ereignisanzeige kann die Kategorie als numerischer Wert dargestellt werden, oder die Kategorie kann als Ressourcenbezeichner zur Anzeige einer lokalisierten Kategoriezeichenfolge verwendet werden. Hinweis |
|---|
Die message-Zeichenfolge darf nicht %n enthalten, wobei n ein ganzzahliger Wert (z. B. "%1") ist, da die Ereignisanzeige sie als Einfügezeichenfolge behandelt. Da eine IPv6 (Internet Protocol, Version 6)-Adresse diese Zeichensequenz enthalten kann, können Sie keine Ereignismeldung protokollieren, die eine IPv6-Adresse enthält. |
Hinweis |
|---|
Der category-Parameter sollte ein positiver Wert sein. Negative Kategoriewerte werden in der Ereignisanzeige als komplementäre positive Zahl angezeigt. Beispielsweise wird -10 als 65.526 und -1 als 65.535 angezeigt. |
Zum Anzeigen von lokalisierten Kategoriezeichenfolgen in der Ereignisanzeige müssen Sie eine mit einer Kategorieressourcendatei konfigurierte Ereignisquelle verwenden und die category auf einen Ressourcenbezeichner in der Kategorieressourcendatei festlegen. Wenn die Ereignisquelle über keine konfigurierte Kategorieressourcendatei verfügt oder die angegebene category keine Zeichenfolge in der Kategorieressourcendatei indiziert, wird in der Ereignisanzeige der numerische Kategoriewert für diesen Eintrag angezeigt. Konfigurieren Sie die Kategorieressourcendatei sowie die Anzahl der Kategoriezeichenfolgen in der Ressourcendatei mithilfe der EventLogInstaller-Klasse oder der EventSourceCreationData-Klasse. Ereignisbezeichner bezeichnen zusammen mit der Ereignisquelle ein Ereignis eindeutig. In jeder Anwendung können zugehörige nummerierte Ereignisse und die Beschreibungszeichenfolgen definiert werden, denen sie zugeordnet sind. Anhand dieser Zeichenfolgenwerte in der Ereignisanzeige können Benutzer leichter erkennen, welche Probleme aufgetreten und welche Maßnahmen zu ergreifen sind. Außerdem können Sie einen EventLogEntryType für das in das Ereignisprotokoll zu schreibende Ereignis angeben. Der type wird in der Ereignisanzeige für ein Protokoll als Symbol und Text in der Spalte Typ angegeben. Dieser Parameter gibt an, ob der Ereignistyp ein Fehler, eine Warnung, eine Information oder eine Erfolgs- bzw. Fehlerüberwachung ist. Sie müssen die Source-Eigenschaft in der EventLog-Komponente festlegen, bevor Sie Einträge in das Protokoll schreiben können. Sie müssen die Ereignisquelle erstellen und konfigurieren, bevor Sie den ersten Eintrag mit der Quelle schreiben. Erstellen Sie die neue Ereignisquelle während der Installation der Anwendung. Dies gibt dem Betriebssystem Gelegenheit, die Liste der registrierten Ereignisquellen und deren Konfiguration zu aktualisieren. Wenn die Liste der Ereignisquellen vom Betriebssystem nicht aktualisiert wurde und Sie versuchen, ein Ereignis für die neue Quelle zu schreiben, schlägt der Schreibvorgang fehl. Sie können eine neue Quelle entweder mithilfe eines EventLogInstaller oder mithilfe der CreateEventSource-Methode erstellen. Sie benötigen Administratorrechte für den Computer, um eine neue Ereignisquelle zu erstellen. Wenn die in der Source-Eigenschaft dieser EventLog-Instanz angegebene Quelle nicht auf dem Computer registriert ist, auf dem die Komponente schreibt, ruft WriteEntry zum Registrieren der Quelle CreateEventSource auf. Wenn das System die Source durch einen Aufruf von WriteEntry registrieren muss und die Log-Eigenschaft nicht auf die EventLog-Instanz festgelegt wurde, wird als Standardprotokoll das Anwendungsprotokoll verwendet. Hinweis |
|---|
Viele der oben aufgeführten Ausnahmen werden durch Fehler generiert, die während der Registrierung der Source aufgetreten sind. |
Die Quelle muss entweder zum Schreiben lokalisierter Einträge oder zum Schreiben direkter Zeichenfolgen konfiguriert werden. Die WriteEntry-Methode schreibt die angegebene Zeichenfolge direkt in das Ereignisprotokoll, ohne eine lokalisierbare Meldungsressourcendatei zu verwenden. Verwenden Sie die WriteEvent-Methode, um Ereignisse mithilfe einer lokalisierten Meldungsressourcendatei zu schreiben. Wenn die Anwendung beim Schreiben von Einträgen sowohl Ressourcenbezeichner als auch Zeichenfolgenwerte verwendet, müssen Sie zwei getrennte Quellen registrieren. Konfigurieren Sie beispielsweise eine Quelle mit Ressourcendateien, und verwenden Sie diese Quelle dann in der WriteEvent-Methode, um Einträge mithilfe von Ressourcenbezeichnern in das Ereignisprotokoll zu schreiben. Erstellen Sie dann eine andere Quelle ohne Ressourcendateien, und verwenden Sie diese Quelle in der WriteEntry-Methode, um Zeichenfolgen mithilfe dieser Quelle direkt in das Ereignisprotokoll zu schreiben. Hinweis |
|---|
Wenn Sie einen Eintrag auf einem Remotecomputer schreiben, ist der Wert der Meldung (die Textzeichenfolge) möglicherweise nicht erwartungsgemäß, wenn auf dem Remotecomputer .NET Framework nicht ausgeführt wird. |
Hinweis |
|---|
Wenn der message-Parameter ein NUL-Zeichen enthält, bricht die Meldung im Ereignisprotokoll nach dem NUL-Zeichen ab. |

Beispiele
' 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)
// 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.
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 );

Versionsinformationen
.NET FrameworkUnterstützt in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileUnterstützt in: 4, 3.5 SP1

.NET Framework-Sicherheit

Plattformen
Windows 7, Windows Vista SP1 oder höher, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core wird nicht unterstützt), Windows Server 2008 R2 (Server Core wird mit SP1 oder höher unterstützt), Windows Server 2003 SP2
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

Siehe auch

Änderungsprotokoll
Datum | Versionsgeschichte | Grund |
|---|
Mai 2010
| Hinweis zur Verwendung von %n im message-Parameter wurde hinzugefügt. |
Korrektur inhaltlicher Fehler.
|
|