共用方式為


EventLog 建構函式

定義

初始化 EventLog 類別的新執行個體。

多載

EventLog()

初始化 EventLog 類別的新執行個體。 不讓執行個體和任何記錄檔產生關聯。

EventLog(String)

初始化 EventLog 類別的新執行個體。 讓執行個體與本機電腦上的記錄檔產生關聯。

EventLog(String, String)

初始化 EventLog 類別的新執行個體。 讓執行個體與指定電腦上的記錄檔產生關聯。

EventLog(String, String, String)

初始化 EventLog 類別的新執行個體。 讓執行個體和指定電腦上的記錄檔產生關聯,並建立或指派指定的來源給 EventLog

EventLog()

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

初始化 EventLog 類別的新執行個體。 不讓執行個體和任何記錄檔產生關聯。

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

範例

如果來源不存在,下列範例會建立來源 MySource ,並將專案寫入事件記錄檔 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

備註

在呼叫 WriteEntry之前,請指定 Source 實例的 EventLog 屬性。 如果您只從記錄檔讀取 Entries ,也可以只 Log 指定 和 MachineName 屬性。

注意

如果您未指定 MachineName,本機計算機會 (“。”假設 ) 。

下表顯示 實例 EventLog的初始屬性值。

屬性 初始值
Source 空字串 ("")。
Log 空字串 ("")。
MachineName 本機計算機 (“。”) 。

另請參閱

適用於

EventLog(String)

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

初始化 EventLog 類別的新執行個體。 讓執行個體與本機電腦上的記錄檔產生關聯。

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

本機電腦上的記錄檔名稱。

例外狀況

記錄檔名稱為 null

記錄檔名稱無效。

範例

下列範例會在本機計算機上讀取事件記錄檔 「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 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

備註

這個多載會將 Log 屬性設定為 logName 參數。 在呼叫 WriteEntry之前,請指定 Source 實例的 EventLog 屬性。 如果您只從記錄檔讀取 Entries ,也可以只 Log 指定 和 MachineName 屬性。

注意

如果您未指定 MachineName,本機計算機會 (“。”假設 ) 。 這個建構函式的多載會 Log 指定 屬性,但您可以在讀取 Entries 屬性之前變更此屬性。

如果您在 屬性中指定的 Source 來源與計算機上的其他來源是唯一的,則後續呼叫 WriteEntry 會建立具有指定名稱的記錄檔,如果該記錄不存在。

下表顯示 實例 EventLog的初始屬性值。

屬性 初始值
Source 空字串 ("")。
Log logName 參數。
MachineName 本機計算機 (“。”) 。

另請參閱

適用於

EventLog(String, String)

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

初始化 EventLog 類別的新執行個體。 讓執行個體與指定電腦上的記錄檔產生關聯。

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

指定電腦上的記錄檔名稱。

machineName
String

記錄檔所在處的電腦。

例外狀況

記錄檔名稱為 null

記錄檔名稱無效。

-或-

電腦名稱無效。

範例

下列範例會在計算機 「myServer」 上讀取事件記錄檔 「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", "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

備註

這個多載會將 Log 屬性設定為 logName 參數, MachineName 並將 屬性設定為 machineName 參數。 在呼叫 WriteEntry之前,請指定 SourceEventLog屬性。 如果您只從記錄檔讀取 Entries ,也可以只 Log 指定 和 MachineName 屬性。

注意

這個建構函式的多載會 Log 指定 和 MachineName 屬性,但您可以在讀取 Entries 屬性之前變更這兩者。

下表顯示 實例 EventLog的初始屬性值。

屬性 初始值
Source 空字串 ("")。
Log logName 參數。
MachineName machineName 參數。

另請參閱

適用於

EventLog(String, String, String)

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

初始化 EventLog 類別的新執行個體。 讓執行個體和指定電腦上的記錄檔產生關聯,並建立或指派指定的來源給 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

指定電腦上的記錄檔名稱。

machineName
String

記錄檔所在處的電腦。

source
String

事件記錄檔項目的來源。

例外狀況

記錄檔名稱為 null

記錄檔名稱無效。

-或-

電腦名稱無效。

範例

下列範例會使用來源 「MySource」 將專案寫入本機電腦上的事件記錄檔 「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( "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

備註

這個建構函式會將 Log 屬性設定為 logName 參數、 MachineName 將 屬性設定為 machineName 參數,並將 Source 屬性設定為 source 參數。 寫入 Source 事件記錄檔時需要 屬性。 不過,如果您只從事件記錄檔讀取,只要伺服器上的事件記錄檔有與其相關聯的來源) ,就只需要 LogMachineName 屬性 (。 如果您只從事件記錄檔讀取,建構函式的另一個多載可能就已足夠。

下表顯示 實例 EventLog的初始屬性值。

屬性 初始值
Source source 參數。
Log logName 參數。
MachineName machineName 參數。

另請參閱

適用於