Export (0) Print
Expand All

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

Writes an entry with the given message text, application-defined event identifier, and application-defined category to the event log (using the specified registered event source) and appends binary data to the message.

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

'Declaration
Public Shared Sub WriteEntry ( _
	source As String, _
	message As String, _
	type As EventLogEntryType, _
	eventID As Integer, _
	category As Short, _
	rawData As Byte() _
)
'Usage
Dim source As String
Dim message As String
Dim type As EventLogEntryType
Dim eventID As Integer
Dim category As Short
Dim rawData As Byte()

EventLog.WriteEntry(source, message, type, eventID, category, rawData)
public static void WriteEntry (
	String source, 
	String message, 
	EventLogEntryType type, 
	int eventID, 
	short category, 
	byte[] rawData
)
public static function WriteEntry (
	source : String, 
	message : String, 
	type : EventLogEntryType, 
	eventID : int, 
	category : short, 
	rawData : byte[]
)

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.

category

The application-specific subcategory associated with the message.

rawData

An array of bytes that holds the binary data associated with the entry.

Exception typeCondition

ArgumentException

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

- or -

The source value is a null reference (Nothing in Visual Basic) (Nothing in Visual Basic).

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

InvalidEnumArgumentException

type is not a valid EventLogEntryType.

InvalidOperationException

The registry key for the event log could not be opened.

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 application-defined event-specific data to the event log, using a source already registered as an event source for the appropriate 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; include it only if you are sure it will be useful. 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.

NoteNote

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 will appear 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, together 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 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.

Dim myByte(9) As Byte
Dim i As Integer
For i = 0 To 9
   myByte(i) = CByte(i Mod 2)
Next i
' Write an informational entry to the event log.
Console.WriteLine("Write from second source ")
EventLog.WriteEntry("SecondSource", "Writing warning to event log.", _
                     EventLogEntryType.Error, myEventID , myCategory , myByte)

ubyte myByte[] = new ubyte[10];
for (int i = 0; i < 10; i++) {
    myByte[i] =( ubyte)(i % 2);
}
// Write an informational entry to the event log.
Console.WriteLine("Write from second source ");
EventLog.WriteEntry("SecondSource", "Writing warning to event log.", 
    EventLogEntryType.Error, myEventID, myCategory, myByte);

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft