Share via


EventLog.EntryWritten Evento

Definição

Ocorre quando uma entrada é gravada em um log de eventos no computador local.

public:
 event System::Diagnostics::EntryWrittenEventHandler ^ EntryWritten;
public event System.Diagnostics.EntryWrittenEventHandler EntryWritten;
member this.EntryWritten : System.Diagnostics.EntryWrittenEventHandler 
Public Custom Event EntryWritten As EntryWrittenEventHandler 

Tipo de evento

Exemplos

O exemplo a seguir manipula um evento escrito de entrada.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
ref class MySample
{
private:

   // This member is used to wait for events.
   static AutoResetEvent^ signal;

public:
   static void main()
   {
      signal = gcnew AutoResetEvent( false );
      EventLog^ myNewLog = gcnew EventLog;
      myNewLog->Source = "testEventLogEvent";
      myNewLog->EntryWritten += gcnew EntryWrittenEventHandler( MyOnEntryWritten );
      myNewLog->EnableRaisingEvents = true;
      myNewLog->WriteEntry("Test message", EventLogEntryType::Information);
      signal->WaitOne();
   }

   static void MyOnEntryWritten( Object^ /*source*/, EntryWrittenEventArgs^ /*e*/ )
   {
      Console::WriteLine("In event handler");
      signal->Set();
   }

};

int main()
{
   MySample::main();
}
using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    // This member is used to wait for events.
    static AutoResetEvent signal;

    public static void Main(){

        signal = new AutoResetEvent(false);
        EventLog myNewLog = new EventLog("Application", ".", "testEventLogEvent");

        myNewLog.EntryWritten += new EntryWrittenEventHandler(MyOnEntryWritten);
        myNewLog.EnableRaisingEvents = true;
        myNewLog.WriteEntry("Test message", EventLogEntryType.Information);
        signal.WaitOne();
    }

    public static void MyOnEntryWritten(object source, EntryWrittenEventArgs e){
        Console.WriteLine("In event handler");
        signal.Set();
    }
}
Option Explicit On 
Option Strict On

Imports System.Diagnostics
Imports System.Threading


Class MySample

    ' This member is used to wait for events.
    Private Shared signal As AutoResetEvent


    Public Shared Sub Main()

        signal = New AutoResetEvent(False)
        Dim myNewLog As New EventLog("Application", ".", "testEventLogEvent")

        AddHandler myNewLog.EntryWritten, AddressOf MyOnEntryWritten
        myNewLog.EnableRaisingEvents = True
        myNewLog.WriteEntry("Test message", EventLogEntryType.Information)

        signal.WaitOne()
    End Sub


    Public Shared Sub MyOnEntryWritten(ByVal [source] As Object, ByVal e As EntryWrittenEventArgs)
        Console.WriteLine("In event handler")
        signal.Set()
    End Sub
End Class

Comentários

Para obter notificações de eventos, você deve definir EnableRaisingEvents como true. Você só pode receber notificações de eventos quando as entradas são gravadas no computador local. Não é possível receber notificações de entradas gravadas em computadores remotos.

Ao criar um EntryWritten delegado, você identifica o método que manipulará o evento. Para associar o evento ao manipulador de eventos, adicione uma instância do delegado ao evento. O manipulador de eventos é chamado sempre que o evento ocorre, até que você remova o delegado. Para obter mais informações sobre como lidar com eventos com delegados, consulte Manipulando e gerando eventos.

O sistema responderá a WriteEntry somente se o último evento de gravação tiver ocorrido pelo menos seis segundos antes. Isso implica que você receberá apenas uma EntryWritten notificação de evento dentro de um intervalo de seis segundos, mesmo que ocorra mais de uma alteração no log de eventos. Se você inserir um intervalo de suspensão suficientemente longo (cerca de 10 segundos) entre as chamadas para WriteEntry, será menos provável que você perca um evento. No entanto, se os eventos de gravação ocorrerem com mais frequência, talvez você não receba a notificação de evento até o próximo intervalo. Normalmente, as notificações de evento perdidas não são perdidas, mas atrasadas.

Aplica-se a

Confira também