//
实例化一个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);
}