//
實例化一個Windows 事件日誌實例
EventLog log1
=
new
EventLog();
private
void
button10_Click(
object
sender, EventArgs e)
{
//
是否存在事件源
if
(
!
EventLog.SourceExists(
"
TestLog
"
))
{
//
建立事件源,創建一個應用程序,使用指定的 Source 做爲向本地計算機上的日
//
志中寫入日誌項的有效事件源,應用程序在本地計算機上。p1註冊時所採用的源名稱,
//
p2源的項寫入的日誌名
EventLog.CreateEventSource(
"
TestLog
"
,
"
log1
"
);
}
//
日誌名稱
log1.Log
=
"
log1
"
;
//
事件源名稱
log1.Source
=
"
TestLog
"
;
//
機器名稱
log1.MachineName
=
"
.
"
;
//
寫入日誌信息,指定類別
log1.WriteEntry(
"
An error has occured
"
, EventLogEntryType.Error);
//
遍歷已存在的日誌信息
foreach
(EventLogEntry item
in
log1.Entries)
{
Console.WriteLine(item.Message
+
"
\t
"
+
item.TimeGenerated);
}