采用以下 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() 没有返回设置事件日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12663703/