Export (0) Print
Expand All

EventLog.WriteEntry Method (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.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

public static void WriteEntry(
	string source,
	string message,
	EventLogEntryType type,
	int eventID
)

Parameters

source
Type: System.String

The source by which the application is registered on the specified computer.

message
Type: System.String

The string to write to the event log.

type
Type: System.Diagnostics.EventLogEntryType

One of the EventLogEntryType values.

eventID
Type: System.Int32

The application-specific identifier for the event.

ExceptionCondition
ArgumentException

The source value is an empty string ("").

- or -

The source value is null.

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

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

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.

NoteNote:

If the message parameter contains a NUL character, the message in the event log is terminated at the NUL character.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft