Export (0) Print
Expand All

EventLog Constructor ()

.NET Framework 1.1

Initializes a new instance of the EventLog class. Does not associate the instance with any log.

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

Remarks

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.

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

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

Example

[Visual Basic, C#, C++] The following example writes an entry to an event log, "MyNewLog", on the local computer, and creates the source "MySource" if it does not already exist.

[Visual Basic, C#, C++] Note   It is not necessary for you to create the event source in your code. If the source you set in the Source property does not already exist, the WriteEntry method creates it before writing to the event log.
[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        
        ' Create the source, if it does not already exist.
        If Not EventLog.SourceExists("MySource") Then
            EventLog.CreateEventSource("MySource", "MyNewLog")
            Console.WriteLine("CreatingEventSource")
        End If
        
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog()
        myLog.Source = "MySource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.")
    End Sub 'Main 
End Class 'MySample

[C#] 
using System;
using System.Diagnostics;
using System.Threading;
              
class MySample{

    public static void Main(){
    
        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource")){
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
        }
                
        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";
        
        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");
        
    }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;

int main(){

    // Create the source, if it does not already exist.
    if(!EventLog::SourceExists(S"MySource")){
        EventLog::CreateEventSource(S"MySource", S"MyNewLog");
        Console::WriteLine(S"CreatingEventSource");
    }

    // Create an EventLog instance and assign its source.
    EventLog* myLog = new EventLog();
    myLog->Source = S"MySource";

    // Write an informational entry to the event log.    
    myLog->WriteEntry(S"Writing to event log.");

}

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

Requirements

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

Show:
© 2015 Microsoft