EventLog 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 EventLog 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| EventLog() |
初始化 EventLog 類別的新執行個體。 不會將實例與任何日誌關聯。 |
| EventLog(String) |
初始化 EventLog 類別的新執行個體。 將實例與本地電腦的日誌關聯起來。 |
| EventLog(String, String) |
初始化 EventLog 類別的新執行個體。 將實例與指定電腦上的日誌關聯起來。 |
| EventLog(String, String, String) |
初始化 EventLog 類別的新執行個體。 將實例與指定電腦上的日誌關聯,並建立或指派指定的來源給 EventLog。 |
EventLog()
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- 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 and MachineName 屬性。
備註
若未指定 , MachineName則假設為本地電腦(「.」)。
下表顯示了 的 EventLog初始屬性值。
| 房產 | 初始值 |
|---|---|
| Source | 一個空字串(“”)。 |
| Log | 一個空字串(“”)。 |
| MachineName | 本地電腦(「.」)。 |
另請參閱
適用於
EventLog(String)
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- 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;
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 and MachineName 屬性。
備註
若未指定 , MachineName則假設為本地電腦(「.」)。 建構子的這個過載會指定該屬性, Log 但你可以在讀取 Entries 屬性前更改它。
如果你在屬性中 Source 指定的來源與電腦上其他來源是唯一的,後續呼叫 WriteEntry 會產生一個包含指定名稱的日誌(如果該日誌尚未存在的話)。
下表顯示了 的 EventLog初始屬性值。
| 房產 | 初始值 |
|---|---|
| Source | 一個空字串(“”)。 |
| Log | 參數 logName 。 |
| MachineName | 本地電腦(「.」)。 |
另請參閱
適用於
EventLog(String, String)
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- 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。
範例
以下範例讀取事件日誌中的條目,「myNewLog」位於電腦「myServer」。
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
備註
此過載將屬性設為LoglogName參數,MachineName屬性設為machineName參數。 在呼叫 WriteEntry之前,先Source指定 的性質。EventLog 如果你只從日誌讀取 Entries ,也可以只指定 Log and MachineName 屬性。
備註
這種建構子的超載會 Log 指定 和 MachineName 屬性,但你可以在讀取 Entries 屬性前更改任何一個。
下表顯示了 的 EventLog初始屬性值。
| 房產 | 初始值 |
|---|---|
| Source | 一個空字串(“”)。 |
| Log | 參數 logName 。 |
| MachineName | 參數 machineName 。 |
另請參閱
適用於
EventLog(String, String, String)
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- EventLog.cs
- 來源:
- 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。
範例
以下範例使用本機的事件日誌「MyNewLog」,使用來源「MySource」寫入。
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參數Sourcesource,屬性為參數。 寫入事件日誌時必須具備該 Source 屬性。 不過,如果你只從事件日誌讀取,只需要 Log 和 MachineName 屬性(只要伺服器上的事件日誌已經有來源連結)。 如果你只從事件日誌讀取,另一個建構子的超載可能就足夠了。
下表顯示了 的 EventLog初始屬性值。
| 房產 | 初始值 |
|---|---|
| Source | 參數 source 。 |
| Log | 參數 logName 。 |
| MachineName | 參數 machineName 。 |