Compartir a través de


EventLog Constructores

Definición

Inicializa una nueva instancia de la clase EventLog.

Sobrecargas

EventLog()

Inicializa una nueva instancia de la clase EventLog. No asocia la instancia a ningún registro.

EventLog(String)

Inicializa una nueva instancia de la clase EventLog. Asocia la instancia a un registro del equipo local.

EventLog(String, String)

Inicializa una nueva instancia de la clase EventLog. Asocia la instancia a un registro en el equipo especificado.

EventLog(String, String, String)

Inicializa una nueva instancia de la clase EventLog. Asocia la instancia a un registro en el equipo especificado y crea o asigna el origen especificado a EventLog.

EventLog()

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

Inicializa una nueva instancia de la clase EventLog. No asocia la instancia a ningún registro.

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

Ejemplos

En el ejemplo siguiente se crea el origen MySource si aún no existe y se escribe una entrada en el registro de eventos MyNewLog.

#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

Comentarios

Antes de llamar a WriteEntry, especifique la Source propiedad de la EventLog instancia. Si solo está leyendo Entries desde el registro, también puede especificar solo las Log propiedades y MachineName .

Nota

Si no especifica un MachineName, se asume el equipo local (".").

En la tabla siguiente se muestran los valores de propiedad iniciales de una instancia de EventLog.

Propiedad Valor inicial
Source Cadena vacía ("").
Log Cadena vacía ("").
MachineName El equipo local (".").

Consulte también

Se aplica a

EventLog(String)

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

Inicializa una nueva instancia de la clase EventLog. Asocia la instancia a un registro del equipo local.

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

Parámetros

logName
String

Nombre del registro en el equipo local.

Excepciones

El nombre del registro es null.

El nombre del registro no es válido.

Ejemplos

En el ejemplo siguiente se leen entradas en el registro de eventos, "myNewLog", en el equipo local.

#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

Comentarios

Esta sobrecarga establece la Log propiedad en el logName parámetro . Antes de llamar a WriteEntry, especifique la Source propiedad de la EventLog instancia. Si solo está leyendo Entries desde el registro, también puede especificar solo las Log propiedades y MachineName .

Nota

Si no especifica un MachineName, se asume el equipo local ("."). Esta sobrecarga del constructor especifica la Log propiedad , pero puede cambiarla antes de leer la Entries propiedad .

Si el origen especificado en la Source propiedad es único de otros orígenes del equipo, una llamada posterior para WriteEntry crear un registro con el nombre especificado, si aún no existe.

En la tabla siguiente se muestran los valores de propiedad iniciales de una instancia de EventLog.

Propiedad Valor inicial
Source Cadena vacía ("").
Log Parámetro logName.
MachineName El equipo local (".").

Consulte también

Se aplica a

EventLog(String, String)

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

Inicializa una nueva instancia de la clase EventLog. Asocia la instancia a un registro en el equipo especificado.

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)

Parámetros

logName
String

Nombre del registro en el equipo especificado.

machineName
String

Equipo en el que existe el registro.

Excepciones

El nombre del registro es null.

El nombre del registro no es válido.

o bien

El nombre de equipo no es válido.

Ejemplos

En el ejemplo siguiente se leen entradas en el registro de eventos, "myNewLog", en el equipo "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

Comentarios

Esta sobrecarga establece la Log propiedad en el logName parámetro y la MachineName propiedad en el machineName parámetro . Antes de llamar a WriteEntry, especifique la Source propiedad de EventLog. Si solo está leyendo Entries desde el registro, también puede especificar solo las Log propiedades y MachineName .

Nota

Esta sobrecarga del constructor especifica las Log propiedades y MachineName , pero puede cambiarla antes de leer la Entries propiedad .

En la tabla siguiente se muestran los valores de propiedad iniciales de una instancia de EventLog.

Propiedad Valor inicial
Source Cadena vacía ("").
Log Parámetro logName.
MachineName Parámetro machineName.

Consulte también

Se aplica a

EventLog(String, String, String)

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

Inicializa una nueva instancia de la clase EventLog. Asocia la instancia a un registro en el equipo especificado y crea o asigna el origen especificado a 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)

Parámetros

logName
String

Nombre del registro en el equipo especificado.

machineName
String

Equipo en el que existe el registro.

source
String

Origen de las entradas del registro de eventos.

Excepciones

El nombre del registro es null.

El nombre del registro no es válido.

o bien

El nombre de equipo no es válido.

Ejemplos

En el ejemplo siguiente se escribe una entrada en un registro de eventos, "MyNewLog", en el equipo local, con el origen "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

Comentarios

Este constructor establece la Log propiedad en el logName parámetro , la MachineName propiedad en el machineName parámetro y la Source propiedad en el source parámetro . La Source propiedad es necesaria al escribir en un registro de eventos. Sin embargo, si solo está leyendo desde un registro de eventos, solo se requieren las Log propiedades y MachineName (siempre que el registro de eventos en el servidor ya tenga asociado un origen). Si solo está leyendo desde el registro de eventos, es posible que sea suficiente otra sobrecarga del constructor.

En la tabla siguiente se muestran los valores de propiedad iniciales de una instancia de EventLog.

Propiedad Valor inicial
Source Parámetro source.
Log Parámetro logName.
MachineName Parámetro machineName.

Consulte también

Se aplica a