Operating system: Windows XP SP3
Development tools: Visual Studio 2008
Language: C # 3.0
. NET framework:3.5
It is often necessary to write the specified information, including exception information and normal processing information, to the log in the program. You can use the EventLog class to write all kinds of information directly to the Windows log in c#3.0. The EventLog class is in the System.Diagnostics namespace. We can view the Windows logs we write in Administrative Tools > Event Viewer, as shown in the following illustration:
The following is an example of writing a log to an application (application) using the EventLog class, which is specified using the EventLogEntryType enumeration type.
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);
}
After you run the above code, you will write the log information as shown in the following illustration.