This documentation is archived and is not being maintained.

EventLog.WriteEntry Method (String, String, EventLogEntryType, Int32)

.NET Framework 1.1

Writes an entry with the given message text and application-defined event identifier to the event log, using the specified registered event source.

[Visual Basic]
Overloads Public Shared Sub WriteEntry( _
   ByVal source As String, _
   ByVal message As String, _
   ByVal type As EventLogEntryType, _
   ByVal eventID As Integer _
)
[C#]
public static void WriteEntry(
 string source,
 string message,
 EventLogEntryType type,
 int eventID
);
[C++]
public: static void WriteEntry(
 String* source,
 String* message,
 EventLogEntryType type,
 int eventID
);
[JScript]
public static function WriteEntry(
   source : String,
 message : String,
 type : EventLogEntryType,
 eventID : int
);

Parameters

source
The source by which the application is registered on the specified computer.
message
The string to write to the event log.
type
One of the EventLogEntryType values.
eventID
The application-specific identifier for the event.

Remarks

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. 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 in the Event column. They are intended to help the user understand what went wrong and suggest what actions to take.

In addition to the event identifier, this overload of WriteEntry lets you 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.

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

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

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

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

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

[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: