日期:2014-05-19  浏览次数:20428 次

如何创建事件日志
我想在异常处理中添加一个事件日志记录功能,应该怎么做?

------解决方案--------------------
.NET Framework 类库
EventLog.WriteEntry 方法 (String)
将信息类型项与给定的消息文本一起写入事件日志。

下面的示例创建源 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 ")){
EventLog.CreateEventSource( "MySource ", "MyNewLog ");
Console.WriteLine( "CreatingEventSource ");
}

// 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. ");

}
}