EventLog Constructors

Definitie

Initialiseert een nieuw exemplaar van de EventLog klasse.

Overloads

Name Description
EventLog()

Initialiseert een nieuw exemplaar van de EventLog klasse. Koppelt het exemplaar niet aan een logboek.

EventLog(String)

Initialiseert een nieuw exemplaar van de EventLog klasse. Koppelt het exemplaar aan een logboek op de lokale computer.

EventLog(String, String)

Initialiseert een nieuw exemplaar van de EventLog klasse. Hiermee koppelt u het exemplaar aan een logboek op de opgegeven computer.

EventLog(String, String, String)

Initialiseert een nieuw exemplaar van de EventLog klasse. Koppelt het exemplaar aan een logboek op de opgegeven computer en maakt of wijst de opgegeven bron toe aan de EventLog.

EventLog()

Initialiseert een nieuw exemplaar van de EventLog klasse. Koppelt het exemplaar niet aan een logboek.

public:
 EventLog();
public EventLog();
Public Sub New ()

Voorbeelden

In het volgende voorbeeld wordt de bron MySource gemaakt als deze nog niet bestaat en schrijft een vermelding naar het gebeurtenislogboek MyNewLog.

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"))
        {
             //An event log source should not be created and immediately used.
             //There is a latency time to enable the source, it should be created
             //prior to executing the application that uses the source.
             //Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatedEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

        // 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.");
    }
}
Option Explicit
Option Strict

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        
        If Not EventLog.SourceExists("MySource") Then
            ' Create the source, if it does not already exist.
            ' An event log source should not be created and immediately used.
            ' There is a latency time to enable the source, it should be created
            ' prior to executing the application that uses the source.
            ' Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog")
            Console.WriteLine("CreatingEventSource")
            'The source is created.  Exit the application to allow it to be registered.
            Return
        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
End Class

Opmerkingen

Voordat u aanroept WriteEntry, geeft u de Source eigenschap van het EventLog exemplaar op. Als u alleen uit het logboek leest Entries , kunt u ook alleen de Log en MachineName eigenschappen opgeven.

Note

Als u geen MachineNameopgeeft, wordt ervan uitgegaan dat de lokale computer (".") wordt gebruikt.

In de volgende tabel ziet u de oorspronkelijke eigenschapswaarden voor een exemplaar van EventLog.

Property Initiële waarde
Source Een lege tekenreeks ("").
Log Een lege tekenreeks ("").
MachineName De lokale computer (".").

Zie ook

Van toepassing op

EventLog(String)

Initialiseert een nieuw exemplaar van de EventLog klasse. Koppelt het exemplaar aan een logboek op de lokale computer.

public:
 EventLog(System::String ^ logName);
public EventLog(string logName);
new System.Diagnostics.EventLog : string -> System.Diagnostics.EventLog
Public Sub New (logName As String)

Parameters

logName
String

De naam van het logboek op de lokale computer.

Uitzonderingen

De logboeknaam is null.

De logboeknaam is ongeldig.

Voorbeelden

In het volgende voorbeeld worden vermeldingen in het gebeurtenislogboek myNewLog op de lokale computer gelezen.

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"))
        {
            //An event log source should not be created and immediately used.
            //There is a latency time to enable the source, it should be created
            //prior to executing the application that uses the source.
            //Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatedEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

        // 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);
        }
    }
}
Option Explicit
Option Strict

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        
        If Not EventLog.SourceExists("MySource") Then
            ' Create the source, if it does not already exist.
            ' An event log source should not be created and immediately used.
            ' There is a latency time to enable the source, it should be created
            ' prior to executing the application that uses the source.
            ' Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog")
            Console.WriteLine("CreatingEventSource")
            'The source is created.  Exit the application to allow it to be registered.
            Return
        End If

        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
End Class

Opmerkingen

Met deze overbelasting wordt de Log eigenschap ingesteld op de logName parameter. Voordat u aanroept WriteEntry, geeft u de Source eigenschap van het EventLog exemplaar op. Als u alleen uit het logboek leest Entries , kunt u ook alleen de Log en MachineName eigenschappen opgeven.

Note

Als u geen MachineNameopgeeft, wordt ervan uitgegaan dat de lokale computer (".") wordt gebruikt. Deze overbelasting van de constructor geeft de Log eigenschap op, maar u kunt deze wijzigen voordat u de Entries eigenschap leest.

Als de bron die u in de Source eigenschap opgeeft uniek is van andere bronnen op de computer, wordt een volgende aanroep voor WriteEntry het maken van een logboek met de opgegeven naam gemaakt, als deze nog niet bestaat.

In de volgende tabel ziet u de oorspronkelijke eigenschapswaarden voor een exemplaar van EventLog.

Property Initiële waarde
Source Een lege tekenreeks ("").
Log De logName parameter.
MachineName De lokale computer (".").

Zie ook

Van toepassing op

EventLog(String, String)

Initialiseert een nieuw exemplaar van de EventLog klasse. Hiermee koppelt u het exemplaar aan een logboek op de opgegeven computer.

public:
 EventLog(System::String ^ logName, System::String ^ machineName);
public EventLog(string logName, string machineName);
new System.Diagnostics.EventLog : string * string -> System.Diagnostics.EventLog
Public Sub New (logName As String, machineName As String)

Parameters

logName
String

De naam van het logboek op de opgegeven computer.

machineName
String

De computer waarop het logboek bestaat.

Uitzonderingen

De logboeknaam is null.

De logboeknaam is ongeldig.

– of –

De computernaam is ongeldig.

Voorbeelden

In het volgende voorbeeld worden vermeldingen in het gebeurtenislogboek 'myNewLog' op de computer 'myServer' gelezen.

using System;
using System.Diagnostics;
using System.Threading;

class MySample1
{
    public static void Main()
    {
        // Create the source, if it does not already exist.
        if (!EventLog.SourceExists("MySource"))
        {
            //An event log source should not be created and immediately used.
            //There is a latency time to enable the source, it should be created
            //prior to executing the application that uses the source.
            //Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog", "myServer");
            Console.WriteLine("CreatedEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }
        // Create an EventLog instance and assign its log name.
        EventLog myLog = new EventLog("myNewLog", "myServer");

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

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        If Not EventLog.SourceExists("MySource") Then
            ' Create the source, if it does not already exist.
            ' An event log source should not be created and immediately used.
            ' There is a latency time to enable the source, it should be created
            ' prior to executing the application that uses the source.
            ' Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog", "myServer")
            Console.WriteLine("CreatingEventSource")
            'The source is created.  Exit the application to allow it to be registered.
            Return
        End If

        ' Create an EventLog instance and assign its log name.
        Dim myLog As New EventLog("myNewLog", "myServer")
        
        ' 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
End Class

Opmerkingen

Met deze overbelasting wordt de Log eigenschap ingesteld op de logName parameter en de MachineName eigenschap op de machineName parameter. Voordat u aanroept WriteEntry, geeft u de Source eigenschap van de EventLog. Als u alleen uit het logboek leest Entries , kunt u ook alleen de Log en MachineName eigenschappen opgeven.

Note

Deze overbelasting van de constructor geeft de Log en MachineName eigenschappen op, maar u kunt deze wijzigen voordat u de Entries eigenschap leest.

In de volgende tabel ziet u de oorspronkelijke eigenschapswaarden voor een exemplaar van EventLog.

Property Initiële waarde
Source Een lege tekenreeks ("").
Log De logName parameter.
MachineName De machineName parameter.

Zie ook

Van toepassing op

EventLog(String, String, String)

Initialiseert een nieuw exemplaar van de EventLog klasse. Koppelt het exemplaar aan een logboek op de opgegeven computer en maakt of wijst de opgegeven bron toe aan de EventLog.

public:
 EventLog(System::String ^ logName, System::String ^ machineName, System::String ^ source);
public EventLog(string logName, string machineName, string source);
new System.Diagnostics.EventLog : string * string * string -> System.Diagnostics.EventLog
Public Sub New (logName As String, machineName As String, source As String)

Parameters

logName
String

De naam van het logboek op de opgegeven computer.

machineName
String

De computer waarop het logboek bestaat.

source
String

De bron van vermeldingen in het gebeurtenislogboek.

Uitzonderingen

De logboeknaam is null.

De logboeknaam is ongeldig.

– of –

De computernaam is ongeldig.

Voorbeelden

In het volgende voorbeeld wordt een vermelding weggeschreven naar een gebeurtenislogboek, 'MyNewLog', op de lokale computer, met behulp van de bron 'MySource'.

using System;
using System.Diagnostics;
using System.Threading;

class MySample2
{
    public static void Main()
    {
        // Create the source, if it does not already exist.
        if (!EventLog.SourceExists("MySource"))
        {
            //An event log source should not be created and immediately used.
            //There is a latency time to enable the source, it should be created
            //prior to executing the application that uses the source.
            //Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatedEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }
        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog("myNewLog", ".", "MySource");

        // Write an entry to the log.
        myLog.WriteEntry("Writing to event log on " + myLog.MachineName);
    }
}
Option Strict
Option Explicit

Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        If Not EventLog.SourceExists("MySource") Then
            ' Create the source, if it does not already exist.
            ' An event log source should not be created and immediately used.
            ' There is a latency time to enable the source, it should be created
            ' prior to executing the application that uses the source.
            ' Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog")
            Console.WriteLine("CreatingEventSource")
            'The source is created.  Exit the application to allow it to be registered.
            Return
        End If
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog("myNewLog", ".", "MySource")
        
        ' Write an entry to the log.        
        myLog.WriteEntry(("Writing to event log on " & myLog.MachineName))
    End Sub
End Class

Opmerkingen

Met deze constructor wordt de Log eigenschap ingesteld op de logName parameter, de MachineName eigenschap op de machineName parameter en de Source eigenschap op de source parameter. De Source eigenschap is vereist bij het schrijven naar een gebeurtenislogboek. Als u echter alleen leest uit een gebeurtenislogboek, zijn alleen de Log eigenschappen en MachineName eigenschappen vereist (zolang het gebeurtenislogboek op de server al een bron heeft gekoppeld). Als u alleen uit het gebeurtenislogboek leest, kan een andere overbelasting van de constructor volstaan.

In de volgende tabel ziet u de oorspronkelijke eigenschapswaarden voor een exemplaar van EventLog.

Property Initiële waarde
Source De source parameter.
Log De logName parameter.
MachineName De machineName parameter.

Zie ook

Van toepassing op