使用C#throw数据读取事件日志文件(.evt)无效
我尝试从C#读取事件日志文件.evt文件,并使用筛选器仅获取Framework 2.0条目:使用C#throw数据读取事件日志文件(.evt)无效,c#,event-log,C#,Event Log,我尝试从C#读取事件日志文件.evt文件,并使用筛选器仅获取Framework 2.0条目: string query = "*[System/Provider/@Name=\"ASP.NET 2.0.50727.0\"]"; var elQuery = new EventLogQuery("C:\evento.evt", PathType.FilePath, query); var elReader = new System.Diagnostics.Eventing.Reader.Even
string query = "*[System/Provider/@Name=\"ASP.NET 2.0.50727.0\"]";
var elQuery = new EventLogQuery("C:\evento.evt", PathType.FilePath, query);
var elReader = new System.Diagnostics.Eventing.Reader.EventLogReader(elQuery);
List<EventRecord> eventList = new List<EventRecord>();
for (EventRecord eventInstance = elReader.ReadEvent(); null != eventInstance; eventInstance = elReader.ReadEvent())
{
string source = eventInstance.ProviderName;
eventList.Add(eventInstance);
if (eventInstance.Properties.Count > 3)
{
string dateTime = eventInstance.Properties[2].Value.ToString();
string message = eventInstance.Properties[1].Value.ToString();
}
}
string query=“*[System/Provider/@Name=\”ASP.NET 2.0.50727.0\”;
var elQuery=neweventlogquery(“C:\evento.evt”,PathType.FilePath,query);
var elReader=new System.Diagnostics.Eventing.Reader.EventLogReader(elQuery);
List eventList=新列表();
例如(EventRecord eventInstance=elReader.ReadEvent();null!=eventInstance;eventInstance=elReader.ReadEvent())
{
string source=eventInstance.ProviderName;
添加(eventInstance);
如果(eventInstance.Properties.Count>3)
{
字符串dateTime=eventInstance.Properties[2].Value.ToString();
字符串消息=eventInstance.Properties[1].Value.ToString();
}
}
如果我放置断点,我会看到一些事件日志条目的正确结果,但如果我按F5,elReader.ReadEvent()抛出错误数据无效
有什么帮助吗?谢谢 在
for
循环中,直到迭代后才检查条件。
因此,这里,elReader.ReadEvent()
将返回null
,并在eventInstance
中设置它。然后循环体将运行,然后它将检查eventInstance
是否为null
并停止
执行这种读取直到null循环的正常方法是使用while
循环,在该循环中设置变量并同时检查条件:
while ((EventRecord eventInstance = elReader.ReadEvent()) != null)
{
// as before
}
这将像以前一样从ReadEvent()
设置eventInstance
,但在运行循环体之前,请检查是否返回了null
此模式在.NET中使用的另一个经典位置是读取文本文件中的行,例如,请参见此 {
你好,Francisco,谢谢你的回答。这可以通过添加有关如何/为什么解决问题的详细信息来改进。请记住,你现在并不是在解决一个人的问题,而是针对所有可能偶然发现此问题的用户。详细信息很重要!
string logType = "Microsoft-Windows-PrintService/Operational";
string query = "*[System/EventID=307]";
var elQuery = new
EventLogQuery(logType, PathType.LogName, query);
var elReader = new EventLogReader(elQuery);
for (EventRecord eventInstance = elReader.ReadEvent(); eventInstance != null; eventInstance = elReader.ReadEvent())
{
string source = eventInstance.ProviderName;
Class1.WriteLog("1:"+eventInstance.Properties[1].Value.ToString() , config.validadirectorio());
Class1.WriteLog("2:" + eventInstance.Properties[2].Value.ToString(), config.validadirectorio());
Class1.WriteLog("3:" + eventInstance.Properties[3].Value.ToString(), config.validadirectorio());
Class1.WriteLog("4:" + eventInstance.Properties[4].Value.ToString(), config.validadirectorio());
Class1.WriteLog("5:" + eventInstance.Properties[5].Value.ToString(), config.validadirectorio());
Class1.WriteLog("6:" + eventInstance.Properties[6].Value.ToString(), config.validadirectorio());
Class1.WriteLog("7:" + eventInstance.Properties[7].Value.ToString(), config.validadirectorio());
}
}