This documentation is archived and is not being maintained.

EventLog Constructor (String)

.NET Framework 1.1

Initializes a new instance of the EventLog class. Associates the instance with a log on the local computer.

[Visual Basic]
Public Sub New( _
   ByVal logName As String _
public EventLog(
   string logName
public: EventLog(
   String* logName
public function EventLog(
   logName : String


The name of the log on the local computer.


This overload sets the Log property to the logName parameter. Before calling WriteEntry, specify the Source property of the EventLog instance. If you are only reading Entries from the log, you can alternatively specify only the Log and MachineName properties.

Note   If you do not specify a MachineName, the local computer (".") is assumed. This overload of the constructor specifies the Log property, but you can change this before reading the Entries property.

If the source you specify in the Source property is unique from other sources on the computer, a subsequent call to WriteEntry creates a log with the specified name, if it does not already exist.

The following table shows initial property values for an instance of EventLog.

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


[Visual Basic, C#, C++] The following example reads entries in the event log, "myNewLog", on the local computer.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.Diagnostics
Imports System.Threading
Imports Microsoft.VisualBasic

Class MySample
    Public Shared Sub Main()
        ' Create an EventLog instance and assign its log name.
        Dim myLog As New EventLog("myNewLog")
        ' Read the event log entries.
        Dim entry As EventLogEntry
        For Each entry In  myLog.Entries
            Console.WriteLine((ControlChars.Tab & "Entry: " & entry.Message))
        Next entry
    End Sub ' Main
End Class ' MySample

using System;
using System.Diagnostics;
using System.Threading;
class MySample{

    public static void Main(){
        // Create an EventLog instance and assign its log name.
        EventLog myLog = new EventLog("myNewLog");

        // Read the event log entries.
        foreach(EventLogEntry entry in myLog.Entries){
            Console.WriteLine("\tEntry: " + entry.Message);

#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;

int main(){

    // Create an EventLog instance and assign its log name.
    EventLog* myLog = new EventLog(S"myNewLog");

    // Read the event log entries.
    System::Collections::IEnumerator* myEnum = myLog->Entries->GetEnumerator();
    while (myEnum->MoveNext())
        EventLogEntry* entry = __try_cast<EventLogEntry*>(myEnum->Current);
        Console::WriteLine(S"\tEntry: {0}", entry->Message);

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


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 Constructor Overload List | Source | Log | MachineName | Entries | WriteEntry | EventLogEntry