Sdílet prostřednictvím


EventLog Konstruktory

Definice

Inicializuje novou instanci EventLog třídy.

Přetížení

Name Description
EventLog()

Inicializuje novou instanci EventLog třídy. Nepřidružuje instanci k žádnému protokolu.

EventLog(String)

Inicializuje novou instanci EventLog třídy. Přidruží instanci k protokolu v místním počítači.

EventLog(String, String)

Inicializuje novou instanci EventLog třídy. Přidruží instanci k protokolu v zadaném počítači.

EventLog(String, String, String)

Inicializuje novou instanci EventLog třídy. Přidruží instanci k protokolu v zadaném počítači a vytvoří nebo přiřadí zadaný zdroj k objektu EventLog.

EventLog()

Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs

Inicializuje novou instanci EventLog třídy. Nepřidružuje instanci k žádnému protokolu.

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

Příklady

Následující příklad vytvoří zdroj MySource , pokud ještě neexistuje, a zapíše položku do protokolu MyNewLogudálostí .

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

Poznámky

Před voláním WriteEntryzadejte Source vlastnost EventLog instance. Pokud čtete Entries pouze z protokolu, můžete alternativně zadat pouze vlastnosti Log a MachineName vlastnosti.

Poznámka:

Pokud neurčíte MachineName, předpokládá se místní počítač (".").

V následující tabulce jsou uvedeny počáteční hodnoty vlastností pro instanci EventLog.

Vlastnictví Počáteční hodnota
Source Prázdný řetězec ("").
Log Prázdný řetězec ("").
MachineName Místní počítač (".").

Viz také

Platí pro

EventLog(String)

Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs

Inicializuje novou instanci EventLog třídy. Přidruží instanci k protokolu v místním počítači.

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

Parametry

logName
String

Název protokolu v místním počítači.

Výjimky

Název protokolu je null.

Název protokolu je neplatný.

Příklady

Následující příklad přečte položky v protokolu událostí "myNewLog" v místním počítači.

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

Poznámky

Toto přetížení nastaví Log vlastnost na logName parametr. Před voláním WriteEntryzadejte Source vlastnost EventLog instance. Pokud čtete Entries pouze z protokolu, můžete alternativně zadat pouze vlastnosti Log a MachineName vlastnosti.

Poznámka:

Pokud neurčíte MachineName, předpokládá se místní počítač ("."). Toto přetížení konstruktoru Log určuje vlastnost, ale můžete to změnit před přečtením Entries vlastnosti.

Pokud je zadaný zdroj ve Source vlastnosti jedinečný z jiných zdrojů v počítači, následné volání pro WriteEntry vytvoření protokolu se zadaným názvem, pokud ještě neexistuje.

V následující tabulce jsou uvedeny počáteční hodnoty vlastností pro instanci EventLog.

Vlastnictví Počáteční hodnota
Source Prázdný řetězec ("").
Log Parametr logName .
MachineName Místní počítač (".").

Viz také

Platí pro

EventLog(String, String)

Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs

Inicializuje novou instanci EventLog třídy. Přidruží instanci k protokolu v zadaném počítači.

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)

Parametry

logName
String

Název protokolu v zadaném počítači.

machineName
String

Počítač, na kterém protokol existuje.

Výjimky

Název protokolu je null.

Název protokolu je neplatný.

nebo

Název počítače je neplatný.

Příklady

Následující příklad přečte položky v protokolu událostí myNewLog v počítači myServer.

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

Poznámky

Toto přetížení nastaví Log vlastnost na logName parametr a MachineName vlastnost na machineName parametr. Před voláním WriteEntryzadejte Source vlastnost EventLog. Pokud čtete Entries pouze z protokolu, můžete alternativně zadat pouze vlastnosti Log a MachineName vlastnosti.

Poznámka:

Toto přetížení konstruktoru Log určuje a MachineName vlastnosti, ale můžete změnit buď před čtením Entries vlastnosti.

V následující tabulce jsou uvedeny počáteční hodnoty vlastností pro instanci EventLog.

Vlastnictví Počáteční hodnota
Source Prázdný řetězec ("").
Log Parametr logName .
MachineName Parametr machineName .

Viz také

Platí pro

EventLog(String, String, String)

Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs
Zdroj:
EventLog.cs

Inicializuje novou instanci EventLog třídy. Přidruží instanci k protokolu v zadaném počítači a vytvoří nebo přiřadí zadaný zdroj k objektu 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)

Parametry

logName
String

Název protokolu v zadaném počítači.

machineName
String

Počítač, na kterém protokol existuje.

source
String

Zdroj položek protokolu událostí.

Výjimky

Název protokolu je null.

Název protokolu je neplatný.

nebo

Název počítače je neplatný.

Příklady

Následující příklad zapíše položku do protokolu událostí "MyNewLog" v místním počítači pomocí zdroje "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

Poznámky

Tento konstruktor nastaví vlastnost na logName parametr, MachineName vlastnost na machineName parametr a Source vlastnost parametrusource.Log Vlastnost Source se vyžaduje při zápisu do protokolu událostí. Pokud ale čtete jenom z protokolu událostí, budou se vyžadovat pouze vlastnosti LogMachineName (pokud má protokol událostí na serveru zdroj, který k němu už je přidružený). Pokud čtete pouze z protokolu událostí, může stačit další přetížení konstruktoru.

V následující tabulce jsou uvedeny počáteční hodnoty vlastností pro instanci EventLog.

Vlastnictví Počáteční hodnota
Source Parametr source .
Log Parametr logName .
MachineName Parametr machineName .

Viz také

Platí pro