c#-GetEventLogs()返回没有安装事件日志?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-GetEventLogs()返回没有安装事件日志?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1411字,纯文字阅读大概需要3分钟。
内容图文
请使用以下C#代码:
EventLog[] eventLogs;
eventLogs = EventLog.GetEventLogs(computername);
foreach (EventLog evt in eventLogs)
{
statusMessagesListBox.Items.Add("evt.Log.ToString(): " + evt.Log.ToString() + "\t\tevt.LogDisplayName: " + evt.LogDisplayName);
}
运行该命令时,输出如下:
evt.Log.ToString(): Application evt.LogDisplayName: Application
evt.Log.ToString(): HardwareEvents evt.LogDisplayName: Hardware Events
evt.Log.ToString(): Security evt.LogDisplayName: Security
依此类推.但是为什么没有安装日志?此外,当我尝试运行此代码时:
var eventLog = new EventLog("Setup", computer);
eventLog.Clear();
eventLog.Dispose();
我收到一条错误消息,即该计算机上确实没有日志“ Setup”.上面的代码适用于除安装程序日志以外的所有其他事件日志.
如何访问安装程序事件日志?
作为参考,尝试的.NET框架为4.0和4.5,目标计算机为Windows 7和2008 R2.
解决方法:
EventLog类仅处理管理事件日志. SetUp事件日志是一个操作日志(您可以在事件查看器中看到此日志),因此此类无法处理.
要访问SetUp事件日志,必须使用System.Diagnostics.Eventing.Reader名称空间中的类.您可以使用以下方法遍历事件:
EventLogQuery query = new EventLogQuery("SetUp", PathType.LogName);
query.ReverseDirection = true; // this tells it to start with newest first
EventLogReader reader = new EventLogReader(query);
EventRecord eventRecord;
while ((eventRecord = reader.ReadEvent()) != null)
{
// each eventRecord is an item from the event log
}
请参阅this MDSN文章,以获取更多详细示例.
内容总结
以上是互联网集市为您收集整理的c#-GetEventLogs()返回没有安装事件日志?全部内容,希望文章能够帮你解决c#-GetEventLogs()返回没有安装事件日志?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。