Compartilhar via


EventLog Construtores

Definição

Inicializa uma nova instância da classe EventLog.

Sobrecargas

EventLog()

Inicializa uma nova instância da classe EventLog. Não associe a instância a nenhum log.

EventLog(String)

Inicializa uma nova instância da classe EventLog. Associa a instância a log no computador local.

EventLog(String, String)

Inicializa uma nova instância da classe EventLog. Associa a instância a um log no computador especificado.

EventLog(String, String, String)

Inicializa uma nova instância da classe EventLog. Associa a instância a um log no computador especificado e cria ou atribui a origem indicada para o EventLog.

EventLog()

Origem:
EventLog.cs
Origem:
EventLog.cs
Origem:
EventLog.cs

Inicializa uma nova instância da classe EventLog. Não associe a instância a nenhum log.

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

Exemplos

O exemplo a seguir cria a origem MySource se ela ainda não existir e grava uma entrada no log 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

Comentários

Antes de chamar WriteEntry, especifique a Source propriedade da EventLog instância . Se você estiver lendo Entries apenas do log, como alternativa, poderá especificar apenas as Log propriedades e MachineName .

Observação

Se você não especificar um MachineName, o computador local (".") será assumido.

A tabela a seguir mostra valores de propriedade iniciais para uma instância do EventLog.

Propriedade Valor inicial
Source Uma cadeia de caracteres vazia ("").
Log Uma cadeia de caracteres vazia ("").
MachineName O computador local (".").

Confira também

Aplica-se a

EventLog(String)

Origem:
EventLog.cs
Origem:
EventLog.cs
Origem:
EventLog.cs

Inicializa uma nova instância da classe EventLog. Associa a instância a log no computador 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

O nome do log no computador local.

Exceções

O nome do log é null.

O nome do log é inválido.

Exemplos

O exemplo a seguir lê entradas no log de eventos, "myNewLog", no computador 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

Comentários

Essa sobrecarga define a Log propriedade como o logName parâmetro . Antes de chamar WriteEntry, especifique a Source propriedade da EventLog instância . Se você estiver lendo Entries apenas do log, como alternativa, poderá especificar apenas as Log propriedades e MachineName .

Observação

Se você não especificar um MachineName, o computador local (".") será assumido. Essa sobrecarga do construtor especifica a Log propriedade , mas você pode alterá-la antes de ler a Entries propriedade .

Se a origem especificada na Source propriedade for exclusiva de outras fontes no computador, uma chamada subsequente para WriteEntry criará um log com o nome especificado, se ele ainda não existir.

A tabela a seguir mostra valores de propriedade iniciais para uma instância do EventLog.

Propriedade Valor inicial
Source Uma cadeia de caracteres vazia ("").
Log O parâmetro logName.
MachineName O computador local (".").

Confira também

Aplica-se a

EventLog(String, String)

Origem:
EventLog.cs
Origem:
EventLog.cs
Origem:
EventLog.cs

Inicializa uma nova instância da classe EventLog. Associa a instância a um log no computador 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

O nome do log no computador especificado.

machineName
String

O computador no qual o log existe.

Exceções

O nome do log é null.

O nome do log é inválido.

- ou -

O nome do computador é inválido.

Exemplos

O exemplo a seguir lê entradas no log de eventos, "myNewLog", no computador "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

Comentários

Essa sobrecarga define a Log propriedade como o logName parâmetro e a MachineName propriedade para o machineName parâmetro . Antes de chamar WriteEntry, especifique a Source propriedade do EventLog. Se você estiver lendo Entries apenas do log, como alternativa, poderá especificar apenas as Log propriedades e MachineName .

Observação

Essa sobrecarga do construtor especifica as Log propriedades e MachineName , mas você pode alterar antes de ler a Entries propriedade .

A tabela a seguir mostra valores de propriedade iniciais para uma instância do EventLog.

Propriedade Valor inicial
Source Uma cadeia de caracteres vazia ("").
Log O parâmetro logName.
MachineName O parâmetro machineName.

Confira também

Aplica-se a

EventLog(String, String, String)

Origem:
EventLog.cs
Origem:
EventLog.cs
Origem:
EventLog.cs

Inicializa uma nova instância da classe EventLog. Associa a instância a um log no computador especificado e cria ou atribui a origem indicada para o 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

O nome do log no computador especificado.

machineName
String

O computador no qual o log existe.

source
String

A origem das entradas de log de eventos.

Exceções

O nome do log é null.

O nome do log é inválido.

- ou -

O nome do computador é inválido.

Exemplos

O exemplo a seguir grava uma entrada em um log de eventos, "MyNewLog", no computador local, usando a origem "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

Comentários

Esse construtor define a Log propriedade como o logName parâmetro , a MachineName propriedade para o machineName parâmetro e a Source propriedade como o source parâmetro . A Source propriedade é necessária ao gravar em um log de eventos. No entanto, se você estiver lendo apenas de um log de eventos, somente as Log propriedades e MachineName serão necessárias (desde que o log de eventos no servidor tenha uma origem já associada a ele). Se você estiver lendo apenas do log de eventos, outra sobrecarga do construtor poderá ser suficiente.

A tabela a seguir mostra valores de propriedade iniciais para uma instância do EventLog.

Propriedade Valor inicial
Source O parâmetro source.
Log O parâmetro logName.
MachineName O parâmetro machineName.

Confira também

Aplica-se a