Export (0) Print
Expand All

EventLog.WriteEntry Method (String, EventLogEntryType, Int32, Int16, Byte[])

.NET Framework 1.1

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.

[Visual Basic]
Overloads Public Sub WriteEntry( _
   ByVal message As String, _
   ByVal type As EventLogEntryType, _
   ByVal eventID As Integer, _
   ByVal category As Short, _
   ByVal rawData() As Byte _
)
[C#]
public void WriteEntry(
 string message,
 EventLogEntryType type,
 int eventID,
 short category,
 byte[] rawData
);
[C++]
public: void WriteEntry(
 String* message,
 EventLogEntryType type,
 int eventID,
 short category,
 unsigned char rawData __gc[]
);
[JScript]
public function WriteEntry(
   message : String,
 type : EventLogEntryType,
 eventID : int,
 category : Int16,
 rawData : Byte[]
);

Parameters

message
The string to write to the event log.
type
One of the EventLogEntryType values.
eventID
The application-specific identifier for the event.
category
The application-specific subcategory associated with the message.
rawData
An array of bytes that holds the binary data associated with the entry.

Exceptions

Exception Type Condition
ArgumentException The Source property of the EventLog has not been set.

-or-

The method attempted to create a new event source, but the computer name is not valid.

-or-

The method attempted to create a new event source, but the source name was not specified.

-or-

The method attempted to create a new event source, but the source already exists on the computer.

-or-

The method attempted to create a new log, but the first eight characters of the log name are not unique on the computer.

-or-

The method attempted to open the event log with read access, but the Log property was not specified.

-or-

The method attempted to open the event log with write access, but the Source property was not specified.

-or-

The message string is too long. The size must be less than 16384 bytes.

-or-

The source is not registered to the given log.

Exception The registry entry for the log could not be opened on a remote computer.
InvalidOperationException The method attempted to open the event log with write access, but you do not have write access to the log.
Win32Exception The method attempted to release the event log's read or write handle but did not do so successfully.

-or-

The method attempted to open the event log with read access but was unable to do so.

-or-

The event could not be reported to the log.

SystemException The event log could not be notified to start checking for events.

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 can use the category to filter events in the log. Each application can define its own numbered categories and the text strings to which they map. The categories must be numbered consecutively beginning with the number 1. The Event Viewer presents the category to the user in the Category column.

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 in the Event column. They are intended 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 in the Event Viewer for a log by an icon and text in the Type column. This 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 writing entries to the log. You can call CreateEventSource on a new source to register it before writing to the event log, but this is not necessary. If the source specified in the Source property of this EventLog instance is not registered on the computer your component is writing to, WriteEntry calls CreateEventSource and registers the source.

Note   If you do not specify a MachineName for your EventLog instance before you call CreateEventSource or WriteEntry, the local computer (".") is assumed.

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, it defaults to the Application log.

Note   Many exceptions listed above are generated by errors raised during the process of registering the Source.

WriteEntry lets you specify a string message to write to the log. This method writes the given string directly to the log; it does not use a localizable message file.

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.

Example

[Visual Basic] 
' 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)

[C#] 
// 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 = 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);

[C++] 
// Create the source, if it does not already exist.
String* myLogName = S"myNewLog";
if (!EventLog::SourceExists(S"MySource")) {
    EventLog::CreateEventSource(S"MySource", myLogName);
    Console::WriteLine(S"Creating EventSource");
} else
    myLogName = EventLog::LogNameFromSourceName(S"MySource", S".");
// Create an EventLog and assign source.
EventLog* myEventLog = new EventLog();
myEventLog->Source = S"MySource";
myEventLog->Log = myLogName;

// Set the 'description' for the event.
String* myMessage = S"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(S"Writing to EventLog.. ");
myEventLog->WriteEntry(myMessage, myEventLogEntryType,
    myApplicatinEventId, myApplicatinCategoryId, myRawData);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace | EventLog.WriteEntry Overload List | EventLogEntryType | Source | CreateEventSource | DeleteEventSource | SourceExists | Entries

Show:
© 2015 Microsoft