EventLog 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 EventLog 类的新实例。
重载
| 名称 | 说明 |
|---|---|
| EventLog() |
初始化 EventLog 类的新实例。 不将实例与任何日志相关联。 |
| EventLog(String) |
初始化 EventLog 类的新实例。 将实例与本地计算机上的日志相关联。 |
| EventLog(String, String) |
初始化 EventLog 类的新实例。 将实例与指定计算机上的日志相关联。 |
| EventLog(String, String, String) |
EventLog()
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
初始化 EventLog 类的新实例。 不将实例与任何日志相关联。
public:
EventLog();
public EventLog();
Public Sub New ()
示例
以下示例创建源(如果该源 MySource 尚不存在),并将一个条目写入事件日志 MyNewLog。
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
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- 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;
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
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- 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;
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 属性。
注释
构造函数的此重载指定 Log 了属性和 MachineName 属性,但可以在读取 Entries 属性之前更改。
下表显示了实例 EventLog的初始属性值。
| 财产 | 初始值 |
|---|---|
| Source | 空字符串(“)。 |
| Log | 参数 logName 。 |
| MachineName | 参数 machineName 。 |
另请参阅
适用于
EventLog(String, String, String)
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
- Source:
- EventLog.cs
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;
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 。 |