使用EventLog类写Windows事件日志

在程序中经常需要将指定的信息(包括异常信息和正常处理信息)写到日志中。在C#3.0中可以使用EventLog类将各种信
 
息直接写入Windows日志。EventLog类在System.Diagnostics命名空间中。我们可以在“管理工具” > "事件查看器“中
 
可以查看我们写入的Windows日志
 
 
下面是一个使用EventLog类向应用程序(Application)写入日志的例子,日志类型使用EventLogEntryType枚举类型指定
 
EventLog log = new EventLog();
try
{
    log.Source = "我的应用程序";
    log.WriteEntry("处理信息1", EventLogEntryType.Information);
    log.WriteEntry("处理信息2", EventLogEntryType.Information);
    throw new System.IO.FileNotFoundException("readme.txt文件未找到");
}
catch (System.IO.FileNotFoundException exception)
{
    log.WriteEntry("处理信息2", EventLogEntryType.Error);
}
原文地址:https://www.cnblogs.com/gjhjoy/p/3510383.html