Share via


EventLog Costruttori

Definizione

Inizializza una nuova istanza della classe EventLog.

Overload

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 log 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 con un log nel computer specificato e crea o assegna l'origine specificata all'oggetto EventLog.

EventLog()

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

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

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

Esempio

Nell'esempio seguente viene creata l'origine MySource se non esiste già e scrive una voce nel registro MyNewLogeventi .

#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( "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( "CreatingEventSource" );
      // The source is created.  Exit the application to allow it to be registered.
      return 0;
   }

   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog;
   myLog->Source = "MySource";
   
   // Write an informational entry to the event log.    
   myLog->WriteEntry( "Writing to event log." );
}
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 . Se si legge Entries solo dal log, è possibile specificare in alternativa solo le Log proprietà e MachineName .

Nota

Se non si specifica un MachineNameoggetto , viene assunto il computer locale (".").

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

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

Vedi anche

Si applica a

EventLog(String)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

Inizializza una nuova istanza della classe EventLog. Associa l'istanza a un log 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

L'esempio seguente legge le voci nel registro eventi"myNewLog" nel computer locale.

#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( "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( "CreatingEventSource" );
      // The source is created.  Exit the application to allow it to be registered.
      return 0;
   }

   
   // Create an EventLog instance and assign its log name.
   EventLog^ myLog = gcnew EventLog( "myNewLog" );
   
   // Read the event log entries.
   System::Collections::IEnumerator^ myEnum = myLog->Entries->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      EventLogEntry^ entry = safe_cast<EventLogEntry^>(myEnum->Current);
      Console::WriteLine( "\tEntry: {0}", entry->Message );
   }
}
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 proprietà sul LoglogName parametro. Prima di chiamare WriteEntry, specificare la Source proprietà dell'istanza EventLog . Se si legge Entries solo dal log, è possibile specificare in alternativa solo le Log proprietà e MachineName .

Nota

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

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

Nella tabella seguente vengono illustrati i valori delle proprietà iniziali 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)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
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 si trova il log.

Eccezioni

Il nome del log è null.

Il nome del log non è valido.

-oppure-

Il nome del computer non è valido.

Esempio

L'esempio seguente legge le voci nel registro eventi "myNewLog", nel computer "myServer".

#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( "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( "CreatingEventSource" );
      // The source is created.  Exit the application to allow it to be registered.
      return 0;
   }

   // Create an EventLog instance and assign its log name.
   EventLog^ myLog = gcnew EventLog( "myNewLog","myServer" );
   
   // Read the event log entries.
   System::Collections::IEnumerator^ myEnum = myLog->Entries->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      EventLogEntry^ entry = safe_cast<EventLogEntry^>(myEnum->Current);
      Console::WriteLine( "\tEntry: {0}", entry->Message );
   }
}
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 legge Entries solo dal log, è possibile specificare in alternativa solo le Log proprietà e MachineName .

Nota

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

Nella tabella seguente vengono illustrati i valori delle proprietà iniziali 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)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

Inizializza una nuova istanza della classe EventLog. Associa l'istanza con 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 si trova il log.

source
String

Origine delle voci del registro eventi.

Eccezioni

Il nome del log è null.

Il nome del log non è valido.

-oppure-

Il nome del computer non è valido.

Esempio

L'esempio seguente scrive una voce in un registro eventi, "MyNewLog", nel computer locale usando l'origine "MySource".

#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( "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( "CreatingEventSource" );
      // The source is created.  Exit the application to allow it to be registered.
      return 0;
   }
   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog( "myNewLog",".","MySource" );
   
   // Write an entry to the log.        
   myLog->WriteEntry( String::Format( "Writing to event log on {0}", myLog->MachineName ) );
}
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 parametro, la MachineName proprietà logNamemachineName sul parametro e la Source proprietà sul source parametro. La Source proprietà è necessaria 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 log 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 delle proprietà iniziali per un'istanza di EventLog.

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

Vedi anche

Si applica a