EventLog 构造函数

定义

初始化 EventLog 类的新实例。

重载

EventLog()

初始化 EventLog 类的新实例。 不将该实例与任何日志关联。

EventLog(String)

初始化 EventLog 类的新实例。 将该实例与本地计算机上的日志关联。

EventLog(String, String)

初始化 EventLog 类的新实例。 将该实例与指定计算机上的日志关联。

EventLog(String, String, String)

初始化 EventLog 类的新实例。 将实例与指定的计算机上的日志关联,并为 EventLog 实例创建或分配指定的源。

EventLog()

Source:
EventLog.cs
Source:
EventLog.cs
Source:
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)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
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)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
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之前, Source 请指定 的 EventLog属性。 如果只是从日志中读取 Entries 数据,也可以仅 Log 指定 和 MachineName 属性。

注意

构造函数的此重载指定 LogMachineName 属性,但你可以在读取属性之前更改任一 Entries 属性。

下表显示了 实例 EventLog的初始属性值。

属性 初始值
Source 空字符串 ("")。
Log logName 参数。
MachineName machineName 参数。

另请参阅

适用于

EventLog(String, String, String)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
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写入事件日志时需要 属性。 但是,如果仅从事件日志中读取数据, Log 则只要服务器上的事件日志具有已与其关联的源) , (只需要 和 MachineName 属性。 如果仅从事件日志中读取数据,则构造函数的另一个重载可能就足够了。

下表显示了 实例 EventLog的初始属性值。

属性 初始值
Source source 参数。
Log logName 参数。
MachineName machineName 参数。

另请参阅

适用于