天天看點

eventlog java_C#3.0使用EventLog類寫Windows事件日志的方法

本文執行個體講述了C#3.0使用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);

}

希望本文所述對大家的C#程式設計有所幫助。