我想捕获异常并将其记录在 Windows 日志文件中。如何打开并写入 Windows 日志?
您可以使用系统.诊断.EventLog.WriteEntry http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.writeentry.aspx函数将条目写入事件日志。
System.Diagnostics.EventLog.WriteEntry("MyEventSource", exception.StackTrace,
System.Diagnostics.EventLogEntryType.Warning);
要读取事件日志,您可以使用系统.诊断.EventLog.GetEventLogs http://msdn.microsoft.com/en-us/library/74e2ybbs.aspx功能。
//here's how you get the event logs
var eventLogs = System.Diagnostics.EventLog.GetEventLogs();
foreach(var eventLog in eventLogs)
{
//here's how you get the event log entries
foreach(var logEntry in eventLog.Entries)
{
//do something with the entry
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)