Condividi tramite


EventLog Costruttori

Definizione

Inizializza una nuova istanza della classe EventLog.

Overload

Nome Descrizione
EventLog()

Inizializza una nuova istanza della classe EventLog. Non associa l'istanza ad alcun log.

EventLog(String)

Inizializza una nuova istanza della classe EventLog. Associa l'istanza a un accesso nel computer locale.

EventLog(String, String)

Inizializza una nuova istanza della classe EventLog. Associa l'istanza a un log nel computer specificato.

EventLog(String, String, String)

Inizializza una nuova istanza della classe EventLog. Associa l'istanza a un log nel computer specificato e crea o assegna l'origine specificata all'oggetto EventLog.

EventLog()

Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs

Inizializza una nuova istanza della classe EventLog. Non associa l'istanza ad alcun log.

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

Esempio

L'esempio seguente crea l'origine MySource se non esiste già e scrive una voce nel registro eventi 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

Commenti

Prima di chiamare WriteEntry, specificare la Source proprietà dell'istanza EventLog di . Se si esegue la sola lettura Entries dal log, in alternativa è possibile specificare solo le Log proprietà e MachineName .

Annotazioni

Se non si specifica un , MachineNameviene utilizzato il computer locale (".").

Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di EventLog.

Proprietà Valore iniziale
Source Stringa vuota ("").
Log Stringa vuota ("").
MachineName Computer locale (".").

Vedi anche

Si applica a

EventLog(String)

Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs

Inizializza una nuova istanza della classe EventLog. Associa l'istanza a un accesso nel computer locale.

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

Parametri

logName
String

Nome del log nel computer locale.

Eccezioni

Il nome del log è null.

Il nome del log non è valido.

Esempio

Nell'esempio seguente vengono lette le voci nel registro eventi "myNewLog" nel computer locale.

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

Commenti

Questo overload imposta la Log proprietà sul logName parametro . Prima di chiamare WriteEntry, specificare la Source proprietà dell'istanza EventLog di . Se si esegue la sola lettura Entries dal log, in alternativa è possibile specificare solo le Log proprietà e MachineName .

Annotazioni

Se non si specifica un , MachineNameviene utilizzato il computer locale ("."). Questo overload del costruttore specifica la Log proprietà , ma è possibile modificarla prima di leggere la Entries proprietà .

Se l'origine specificata nella Source proprietà è univoca da altre origini nel computer, una chiamata successiva a WriteEntry crea un log con il nome specificato, se non esiste già.

Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di EventLog.

Proprietà Valore iniziale
Source Stringa vuota ("").
Log Parametro logName .
MachineName Computer locale (".").

Vedi anche

Si applica a

EventLog(String, String)

Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs

Inizializza una nuova istanza della classe EventLog. Associa l'istanza a un log nel computer specificato.

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)

Parametri

logName
String

Nome del log nel computer specificato.

machineName
String

Computer in cui è presente il log.

Eccezioni

Il nome del log è null.

Il nome del log non è valido.

oppure

Nome computer non valido.

Esempio

Nell'esempio seguente vengono lette le voci nel registro eventi "myNewLog" nel computer "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

Commenti

Questo overload imposta la Log proprietà sul logName parametro e la MachineName proprietà sul machineName parametro . Prima di chiamare WriteEntry, specificare la Source proprietà dell'oggetto EventLog. Se si esegue la sola lettura Entries dal log, in alternativa è possibile specificare solo le Log proprietà e MachineName .

Annotazioni

Questo overload del costruttore specifica le Log proprietà e MachineName , ma è possibile modificarle prima di leggere la Entries proprietà.

Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di EventLog.

Proprietà Valore iniziale
Source Stringa vuota ("").
Log Parametro logName .
MachineName Parametro machineName .

Vedi anche

Si applica a

EventLog(String, String, String)

Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs
Origine:
EventLog.cs

Inizializza una nuova istanza della classe EventLog. Associa l'istanza a un log nel computer specificato e crea o assegna l'origine specificata all'oggetto 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)

Parametri

logName
String

Nome del log nel computer specificato.

machineName
String

Computer in cui è presente il log.

source
String

Origine delle voci del registro eventi.

Eccezioni

Il nome del log è null.

Il nome del log non è valido.

oppure

Nome computer non valido.

Esempio

L'esempio seguente scrive una voce in un registro eventi, "MyNewLog", nel computer locale, usando l'origine "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

Commenti

Questo costruttore imposta la Log proprietà sul logName parametro , la MachineName proprietà sul machineName parametro e la Source proprietà sul source parametro . La Source proprietà è obbligatoria durante la scrittura in un registro eventi. Tuttavia, se si legge solo da un registro eventi, sono necessarie solo le Log proprietà e MachineName , purché il registro eventi nel server abbia già associato un'origine. Se si legge solo dal registro eventi, potrebbe essere sufficiente un altro overload del costruttore.

Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di EventLog.

Proprietà Valore iniziale
Source Parametro source .
Log Parametro logName .
MachineName Parametro machineName .

Vedi anche

Si applica a