Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

EventLog.WriteEntry Method (String, EventLogEntryType, Int32)

Writes an entry with the given message text and application-defined event identifier to the event log.

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

'Declaration
Public Sub WriteEntry ( _
	message As String, _
	type As EventLogEntryType, _
	eventID As Integer _
)
'Usage
Dim instance As EventLog
Dim message As String
Dim type As EventLogEntryType
Dim eventID As Integer

instance.WriteEntry(message, type, eventID)
public void WriteEntry (
	String message, 
	EventLogEntryType type, 
	int eventID
)
public function WriteEntry (
	message : String, 
	type : EventLogEntryType, 
	eventID : int
)

Parameters

message

The string to write to the event log.

type

One of the EventLogEntryType values.

eventID

The application-specific identifier for the event.

Exception typeCondition

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.

Use this method to write an entry with an application-defined eventID to the event log. The eventID together 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 display these string values to help the user understand what went wrong and suggest what actions to take.

In addition to the event identifier, 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.

NoteNote

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

NoteNote

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.

NoteNote

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.

NoteNote

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

Dim myRemoteMachine As String
Dim myLogName as string = "MyLog"
Console.Write("Enter computer on which to create log : ")
myRemoteMachine = Console.ReadLine()

' Check if the source exists.
if not EventLog.SourceExists("MyTestSource") then
   'Create source.
   EventLog.CreateEventSource("MyTestSource", myLogName,myRemoteMachine)
   Console.WriteLine("Creating EventSource")
else
   'Get the EventLog associated if the source exists.
   myLogName = EventLog.LogNameFromSourceName("MyTestSource",myRemoteMachine)
end if

Dim myEventLog1 As New EventLog(myLogName, myRemoteMachine)
myEventLog1.Source = "MyTestSource"
' Write an entry into log.
myEventLog1.WriteEntry("This is for your information", _
                     EventLogEntryType.SuccessAudit, 100)
Console.WriteLine("An EventLog created on computer " + myEventLog1.MachineName)

String myRemoteMachine;
String myLogName = "MyLog";

Console.Write("Enter computer on which to create log : ");
myRemoteMachine = Console.ReadLine();

// Check if the source exists.
if (!(EventLog.SourceExists("MyTestSource"))) {
    //Create source.
    EventLog.CreateEventSource("MyTestSource",
        myLogName, myRemoteMachine);
    Console.WriteLine("Creating EventSource");
}                
else {
    // Get the EventLog associated if the source exists.
    myLogName = EventLog.LogNameFromSourceName("MyTestSource",
        myRemoteMachine);
}

EventLog myEventLog1 = new EventLog(myLogName, myRemoteMachine);

myEventLog1.set_Source("MyTestSource");

// Write an entry into log.
myEventLog1.WriteEntry("This is for your information",
    EventLogEntryType.SuccessAudit, 100);
Console.WriteLine("An EventLog created on computer " 
    + myEventLog1.get_MachineName());

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.