NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

EventLog Constructor ()

Initializes a new instance of the EventLog class.

[Visual Basic]
Overloads Public Sub New()
[C#]
public EventLog();
[C++]
public: EventLog();
[JScript]
public function EventLog();

Remarks

Use the EventLog constructor to create an object to read or write entries to a log, or to clear or close a particular log. Before calling any of these methods, associate a Source with your event log object in order to connect it to a particular log in the Windows 2000 event logs. If you will only be reading Entries from the log, you can alternately specify only the Log and MachineName.

Note   If you do not specify a MachineName, the local computer (".") will be assumed.

You do not need to create an instance of EventLog to use the methods CreateEventSource, Delete, DeleteEventSource, Exists, GetEventLogs, or SourceExists. These are all static (in Visual Basic Shared) members, so they are called on the EventLog class itself, for example, EventLog.Delete(myLog).

When an instance of EventLog is created, the following read/write properties are set to initial values.

Property Initial Value
Source An empty string ("")
Log An empty string
MachineName The local computer (".")

You can change the value for any of these properties through a separate call to the property.

Example [Visual Basic]

The following example connects to an event log, "myNewLog", on the local computer. If the source "myNewSource" does not already exist on the computer, it is created. Then, an entry is written to the Log.

Note that the way to create a new event log is through the static (Shared) member CreateEventSource, which creates a Source and registers it to the Log at the time the log is created. Because this is a static (Shared) member of EventLog, it should be called on the class itself rather than on an instance of the class.

Import the System.Diagnostics namespace for this example.

[Visual Basic]

Private Sub ConnectToEventLog()
    'Declare a new event log and event log entry.
    Dim evtLog As EventLog

    'Instantiate a new event log.
    evtLog = New EventLog

    'Otherwise, create a new source and log.
    If Not EventLog.SourceExists("MyNewSource") Then
        'If MyNewLog does not exist, it will be created
        EventLog.CreateEventSource("MyNewSource", "MyNewLog")
    End If
    
    'Connect the source to the event instance.
    evtLog.Source = "MyNewSource"
    
    'Write an entry to the event log.
    evtLog.WriteEntry("My entry.")
End Sub

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace | EventLog Constructor Overload List | Source | Log | MachineName | Entries | Clear | Close | WriteEntry | EventLogEntry