EventLog Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy EventLog.

Przeciążenia

EventLog()

Inicjuje nowe wystąpienie klasy EventLog. Nie kojarzy wystąpienia z żadnym dziennikiem.

EventLog(String)

Inicjuje nowe wystąpienie klasy EventLog. Kojarzy wystąpienie z dziennikiem na komputerze lokalnym.

EventLog(String, String)

Inicjuje nowe wystąpienie klasy EventLog. Kojarzy wystąpienie z dziennikiem na określonym komputerze.

EventLog(String, String, String)

Inicjuje nowe wystąpienie klasy EventLog. Kojarzy wystąpienie z dziennikiem na określonym komputerze i tworzy lub przypisuje określone źródło do obiektu EventLog.

EventLog()

Źródło:
EventLog.cs
Źródło:
EventLog.cs
Źródło:
EventLog.cs

Inicjuje nowe wystąpienie klasy EventLog. Nie kojarzy wystąpienia z żadnym dziennikiem.

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

Przykłady

Poniższy przykład tworzy źródło MySource , jeśli jeszcze nie istnieje, i zapisuje wpis w dzienniku MyNewLogzdarzeń .

#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

Uwagi

Przed wywołaniem WriteEntrymetody określ Source właściwość EventLog wystąpienia. Jeśli odczytujesz Entries tylko z dziennika, możesz również określić tylko Log właściwości i MachineName .

Uwaga

Jeśli nie określisz MachineNameelementu , zakłada się, że komputer lokalny (".").

W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia klasy EventLog.

Właściwość Wartość początkowa
Source Ciąg pusty ("").
Log Ciąg pusty ("").
MachineName Komputer lokalny (".").

Zobacz też

Dotyczy

EventLog(String)

Źródło:
EventLog.cs
Źródło:
EventLog.cs
Źródło:
EventLog.cs

Inicjuje nowe wystąpienie klasy EventLog. Kojarzy wystąpienie z dziennikiem na komputerze lokalnym.

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

Parametry

logName
String

Nazwa dziennika na komputerze lokalnym.

Wyjątki

Nazwa dziennika to null.

Nazwa dziennika jest nieprawidłowa.

Przykłady

Poniższy przykład odczytuje wpisy w dzienniku zdarzeń "myNewLog" na komputerze lokalnym.

#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

Uwagi

To przeciążenie ustawia Log właściwość na logName parametr . Przed wywołaniem WriteEntrymetody określ Source właściwość EventLog wystąpienia. Jeśli odczytujesz Entries tylko z dziennika, możesz również określić tylko Log właściwości i MachineName .

Uwaga

Jeśli nie określisz MachineNameelementu , zakłada się, że komputer lokalny ("."). To przeciążenie konstruktora określa Log właściwość, ale można to zmienić przed odczytaniem Entries właściwości.

Jeśli źródło określone we Source właściwości jest unikatowe z innych źródeł na komputerze, kolejne wywołanie tworzenia WriteEntry dziennika o określonej nazwie, jeśli jeszcze nie istnieje.

W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia klasy EventLog.

Właściwość Wartość początkowa
Source Ciąg pusty ("").
Log Parametr logName .
MachineName Komputer lokalny (".").

Zobacz też

Dotyczy

EventLog(String, String)

Źródło:
EventLog.cs
Źródło:
EventLog.cs
Źródło:
EventLog.cs

Inicjuje nowe wystąpienie klasy EventLog. Kojarzy wystąpienie z dziennikiem na określonym komputerze.

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)

Parametry

logName
String

Nazwa dziennika na określonym komputerze.

machineName
String

Komputer, na którym istnieje dziennik.

Wyjątki

Nazwa dziennika to null.

Nazwa dziennika jest nieprawidłowa.

-lub-

Nazwa komputera jest nieprawidłowa.

Przykłady

Poniższy przykład odczytuje wpisy w dzienniku zdarzeń "myNewLog" na komputerze "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

Uwagi

To przeciążenie ustawia Log właściwość na logName parametr i MachineName właściwość na machineName parametr . Przed wywołaniem WriteEntrymetody określ Source właściwość EventLog. Jeśli odczytujesz Entries tylko z dziennika, możesz również określić tylko Log właściwości i MachineName .

Uwaga

To przeciążenie konstruktora określa Log właściwości i MachineName , ale można je zmienić przed odczytaniem Entries właściwości.

W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia klasy EventLog.

Właściwość Wartość początkowa
Source Ciąg pusty ("").
Log Parametr logName .
MachineName Parametr machineName .

Zobacz też

Dotyczy

EventLog(String, String, String)

Źródło:
EventLog.cs
Źródło:
EventLog.cs
Źródło:
EventLog.cs

Inicjuje nowe wystąpienie klasy EventLog. Kojarzy wystąpienie z dziennikiem na określonym komputerze i tworzy lub przypisuje określone źródło do obiektu 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)

Parametry

logName
String

Nazwa dziennika na określonym komputerze.

machineName
String

Komputer, na którym istnieje dziennik.

source
String

Źródło wpisów dziennika zdarzeń.

Wyjątki

Nazwa dziennika to null.

Nazwa dziennika jest nieprawidłowa.

-lub-

Nazwa komputera jest nieprawidłowa.

Przykłady

Poniższy przykład zapisuje wpis w dzienniku zdarzeń "MyNewLog" na komputerze lokalnym przy użyciu źródła "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

Uwagi

Ten konstruktor ustawia właściwość na logName parametr, MachineName właściwość na machineName parametr i Source właściwość parametrusource.Log Właściwość jest wymagana podczas zapisywania Source w dzienniku zdarzeń. Jeśli jednak odczytujesz tylko z dziennika zdarzeń, wymagane są tylko Log właściwości i MachineName (o ile dziennik zdarzeń na serwerze ma już skojarzone źródło). Jeśli odczytujesz tylko z dziennika zdarzeń, może wystarczyć kolejne przeciążenie konstruktora.

W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia klasy EventLog.

Właściwość Wartość początkowa
Source Parametr source .
Log Parametr logName .
MachineName Parametr machineName .

Zobacz też

Dotyczy