EventLog EventLog EventLog EventLog Constructors

定義

多載

EventLog() EventLog() EventLog()

初始化 EventLog 類別的新執行個體。Initializes a new instance of the EventLog class. 不讓執行個體和任何記錄檔產生關聯。Does not associate the instance with any log.

EventLog(String) EventLog(String) EventLog(String) EventLog(String)

初始化 EventLog 類別的新執行個體。Initializes a new instance of the EventLog class. 讓執行個體與本機電腦上的記錄檔產生關聯。Associates the instance with a log on the local computer.

EventLog(String, String) EventLog(String, String) EventLog(String, String) EventLog(String, String)

初始化 EventLog 類別的新執行個體。Initializes a new instance of the EventLog class. 讓執行個體與指定電腦上的記錄檔產生關聯。Associates the instance with a log on the specified computer.

EventLog(String, String, String) EventLog(String, String, String) EventLog(String, String, String) EventLog(String, String, String)

初始化 EventLog 類別的新執行個體。Initializes a new instance of the EventLog class. 讓執行個體和指定電腦上的記錄檔產生關聯,並建立或指派指定的來源給 EventLogAssociates the instance with a log on the specified computer and creates or assigns the specified source to the EventLog.

EventLog() EventLog() EventLog()

初始化 EventLog 類別的新執行個體。Initializes a new instance of the EventLog class. 不讓執行個體和任何記錄檔產生關聯。Does not associate the instance with any log.

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

範例

下列範例會建立來源MySource如果不存在,它的項目寫入事件記錄檔MyNewLogThe following example creates the source MySource if it does not already exist, and writes an entry to the event log 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
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 'Main 
End Class 'MySample

備註

然後再呼叫WriteEntry,指定Source屬性EventLog執行個體。Before calling WriteEntry, specify the Source property of the EventLog instance. 如果您僅讀取Entries從記錄檔中,您也可以指定僅LogMachineName屬性。If you are only reading Entries from the log, you can alternatively specify only the Log and MachineName properties.

注意

如果您未指定MachineName,本機電腦 ("。") 會假設。If you do not specify a MachineName, the local computer (".") is assumed.

下表顯示的執行個體的初始屬性值EventLogThe following table shows initial property values for an instance of EventLog.

屬性Property 初始值Initial Value
Source 空字串 ("")。An empty string ("").
Log 空字串 ("")。An empty string ("").
MachineName 在本機電腦 ("。")。The local computer (".").

安全性

EventLogPermission
用於寫入在電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉型別: WriteAssociated enumeration: Write

另請參閱

EventLog(String) EventLog(String) EventLog(String) EventLog(String)

初始化 EventLog 類別的新執行個體。Initializes a new instance of the EventLog class. 讓執行個體與本機電腦上的記錄檔產生關聯。Associates the instance with a log on the local computer.

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

參數

logName
String String String String

本機電腦上的記錄檔名稱。The name of the log on the local computer.

例外狀況

記錄檔名稱無效。The log name is invalid.

範例

下列範例會讀取事件記錄檔,而"myNewLog 」,在本機電腦中的項目。The following example reads entries in the event log, "myNewLog", on the local computer.

#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
Imports System.Diagnostics
Imports System.Threading
Imports Microsoft.VisualBasic

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 ' Main
End Class ' MySample

備註

設定這個多載Log屬性設logName參數。This overload sets the Log property to the logName parameter. 然後再呼叫WriteEntry,指定Source屬性EventLog執行個體。Before calling WriteEntry, specify the Source property of the EventLog instance. 如果您僅讀取Entries從記錄檔中,您也可以指定僅LogMachineName屬性。If you are only reading Entries from the log, you can alternatively specify only the Log and MachineName properties.

注意

如果您未指定MachineName,本機電腦 ("。") 會假設。If you do not specify a MachineName, the local computer (".") is assumed. 這個多載的建構函式指定Log屬性,但您可以變更此讀取前Entries屬性。This overload of the constructor specifies the Log property, but you can change this before reading the Entries property.

如果您在指定的來源Source屬性是唯一的電腦,後續呼叫上的其他來源WriteEntry建立記錄,其指定的名稱,如果不存在。If the source you specify in the Source property is unique from other sources on the computer, a subsequent call to WriteEntry creates a log with the specified name, if it does not already exist.

下表顯示的執行個體的初始屬性值EventLogThe following table shows initial property values for an instance of EventLog.

屬性Property 初始值Initial Value
Source 空字串 ("")。An empty string ("").
Log logName 參數。The logName parameter.
MachineName 在本機電腦 ("。")。The local computer (".").

安全性

EventLogPermission
用於寫入在電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉型別: WriteAssociated enumeration: Write

另請參閱

EventLog(String, String) EventLog(String, String) EventLog(String, String) EventLog(String, String)

初始化 EventLog 類別的新執行個體。Initializes a new instance of the EventLog class. 讓執行個體與指定電腦上的記錄檔產生關聯。Associates the instance with a log on the specified computer.

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)

參數

logName
String String String String

指定電腦上的記錄檔名稱。The name of the log on the specified computer.

machineName
String String String String

記錄檔所在處的電腦。The computer on which the log exists.

例外狀況

記錄檔名稱無效。The log name is invalid.

-或--or-

電腦名稱無效。The computer name is invalid.

範例

下列範例會讀取事件記錄檔,而 「 myNewLog","myServer"的電腦上的項目。The following example reads entries in the event log, "myNewLog", on the computer "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 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", "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
Imports System.Diagnostics
Imports System.Threading
Imports Microsoft.VisualBasic

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 ' Main
End Class ' MySample

備註

設定這個多載Log屬性,以logName參數和MachineName屬性設machineName參數。This overload sets the Log property to the logName parameter and the MachineName property to the machineName parameter. 然後再呼叫WriteEntry,指定Source屬性EventLogBefore calling WriteEntry, specify the Source property of the EventLog. 如果您僅讀取Entries從記錄檔中,您也可以指定僅LogMachineName屬性。If you are only reading Entries from the log, you can alternatively specify only the Log and MachineName properties.

注意

這個多載的建構函式指定LogMachineName屬性,但是您可以變更之前讀取Entries屬性。This overload of the constructor specifies the Log and MachineName properties, but you can change either before reading the Entries property.

下表顯示的執行個體的初始屬性值EventLogThe following table shows initial property values for an instance of EventLog.

屬性Property 初始值Initial Value
Source 空字串 ("")。An empty string ("").
Log logName 參數。The logName parameter.
MachineName machineName 參數。The machineName parameter.

安全性

EventLogPermission
用於寫入在電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉型別: WriteAssociated enumeration: Write

另請參閱

EventLog(String, String, String) EventLog(String, String, String) EventLog(String, String, String) EventLog(String, String, String)

初始化 EventLog 類別的新執行個體。Initializes a new instance of the EventLog class. 讓執行個體和指定電腦上的記錄檔產生關聯,並建立或指派指定的來源給 EventLogAssociates the instance with a log on the specified computer and creates or assigns the specified source to the 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)

參數

logName
String String String String

指定電腦上的記錄檔名稱The name of the log on the specified computer

machineName
String String String String

記錄檔所在處的電腦。The computer on which the log exists.

source
String String String String

事件記錄檔項目的來源。The source of event log entries.

例外狀況

記錄檔名稱無效。The log name is invalid.

-或--or-

電腦名稱無效。The computer name is invalid.

範例

下列範例會的項目寫入事件記錄檔時,「 MyNewLog",本機電腦上,使用"MySource"的來源。The following example writes an entry to an event log, "MyNewLog", on the local computer, using the source "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 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("myNewLog", ".", "MySource");

        // Write an entry to the log.        
        myLog.WriteEntry("Writing to event log on " + myLog.MachineName);

    }
}
   
Option Strict
Option Explicit

Imports System
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 ' Main 
End Class ' MySample

備註

這個建構函式設定Log屬性,以logName參數,MachineName屬性設machineName參數,而Source屬性設source參數。This constructor sets the Log property to the logName parameter, the MachineName property to the machineName parameter, and the Source property to the source parameter. Source寫入事件記錄檔時,就需要屬性。The Source property is required when writing to an event log. 不過,如果您僅讀取事件記錄檔,只有LogMachineName(只要事件記錄檔,在伺服器上都有一個已經與它關聯的來源),都需要屬性。However, if you are only reading from an event log, only the Log and MachineName properties are required (as long as the event log on the server has a source already associated with it). 如果您僅讀取事件記錄檔,另一個多載的建構函式可能已足夠。If you are only reading from the event log, another overload of the constructor might suffice.

下表顯示的執行個體的初始屬性值EventLogThe following table shows initial property values for an instance of EventLog.

屬性Property 初始值Initial Value
Source source 參數。The source parameter.
Log logName 參數。The logName parameter.
MachineName machineName 參數。The machineName parameter.

安全性

EventLogPermission
用於寫入在電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉型別: WriteAssociated enumeration: Write

另請參閱

適用於