EventLog Construtores

Definição

Inicializa uma nova instância da EventLog classe.

Sobrecargas

Name Description
EventLog()

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

EventLog(String)

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

EventLog(String, String)

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

EventLog(String, String, String)

Inicializa uma nova instância da EventLog classe. Associa a instância a um log no computador especificado e cria ou atribui a fonte especificada ao EventLog.

EventLog()

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

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

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

Exemplos

O exemplo seguinte cria a fonte MySource se esta ainda não existir, e escreve uma entrada no registo MyNewLogde eventos .

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

Observações

Antes de chamar WriteEntry, especifique a Source propriedade da EventLog instância. Se estiver apenas a ler Entries do log, pode especificar apenas as Log propriedades e.MachineName

Note

Se não especificar um MachineName, assume-se que o computador local (".") é assumido.

A tabela seguinte mostra os valores iniciais das propriedades para uma instância de EventLog.

Property Valor Inicial
Source Uma corda vazia ("").
Log Uma corda vazia ("").
MachineName O computador local (".").

Ver também

Aplica-se a

EventLog(String)

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

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

Exceções

O nome do registo é null.

O nome do registo é inválido.

Exemplos

O exemplo seguinte lê entradas no registo de eventos, "myNewLog", no computador local.

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

Observações

Esta sobrecarga define a Log propriedade para o logName parâmetro. Antes de chamar WriteEntry, especifique a Source propriedade da EventLog instância. Se estiver apenas a ler Entries do log, pode especificar apenas as Log propriedades e.MachineName

Note

Se não especificar um MachineName, assume-se que o computador local (".") é assumido. Esta sobrecarga do construtor especifica a Log propriedade, mas pode alterar isso antes de ler a Entries propriedade.

Se a fonte que especifica na Source propriedade for única em relação a outras fontes no computador, uma chamada subsequente cria WriteEntry um registo com o nome especificado, caso ainda não exista.

A tabela seguinte mostra os valores iniciais das propriedades para uma instância de EventLog.

Property Valor Inicial
Source Uma corda vazia ("").
Log O logName parâmetro.
MachineName O computador local (".").

Ver também

Aplica-se a

EventLog(String, String)

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

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

machineName
String

O computador onde o registo existe.

Exceções

O nome do registo é null.

O nome do registo é inválido.

-ou-

O nome do computador é inválido.

Exemplos

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

Observações

Esta sobrecarga define a Log propriedade para o logName parâmetro e a MachineName propriedade para o machineName parâmetro. Antes de chamar WriteEntry, especifique a Source propriedade do EventLog. Se estiver apenas a ler Entries do log, pode especificar apenas as Log propriedades e.MachineName

Note

Esta sobrecarga do construtor especifica as Log propriedades e, MachineName mas pode alterar qualquer uma antes de ler a Entries propriedade.

A tabela seguinte mostra os valores iniciais das propriedades para uma instância de EventLog.

Property Valor Inicial
Source Uma corda vazia ("").
Log O logName parâmetro.
MachineName O machineName parâmetro.

Ver também

Aplica-se a

EventLog(String, String, String)

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

Inicializa uma nova instância da EventLog classe. Associa a instância a um log no computador especificado e cria ou atribui a fonte especificada ao 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 registo no computador especificado.

machineName
String

O computador onde o registo existe.

source
String

A origem das entradas do registo de eventos.

Exceções

O nome do registo é null.

O nome do registo é inválido.

-ou-

O nome do computador é inválido.

Exemplos

O exemplo seguinte escreve uma entrada num registo de eventos, "MyNewLog", no computador local, usando a fonte "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

Observações

Este construtor define a Log propriedade para o logName parâmetro, a MachineName propriedade para o machineName parâmetro e a Source propriedade para o source parâmetro. A Source propriedade é necessária ao escrever num registo de eventos. No entanto, se estiver apenas a ler um registo de eventos, apenas as Log propriedades e MachineName são necessárias (desde que o registo de eventos no servidor já tenha uma fonte associada). Se estiveres apenas a ler do registo de eventos, outra sobrecarga do construtor pode ser suficiente.

A tabela seguinte mostra os valores iniciais das propriedades para uma instância de EventLog.

Property Valor Inicial
Source O source parâmetro.
Log O logName parâmetro.
MachineName O machineName parâmetro.

Ver também

Aplica-se a