Read event viewer entries

前端 未结 1 1633
梦谈多话
梦谈多话 2021-01-22 12:23

I want to read event entries from a certain custom event log at c# program, And to filter them by their description. Is there a way to do it? Or a way to get the entries as co

相关标签:
1条回答
  • 2021-01-22 13:11

    Try something like this:

           string queryString = string.Format("*[System[TimeCreated[@SystemTime>='{0}' and @SystemTime<='{1}']]]",
                DateTime.Now.Date.AddDays(-10).ToString("s"),
                DateTime.Now.Date.ToString("s"));
            var q = new EventLogQuery("Microsoft-Windows-User Profile Service/Operational", PathType.LogName, queryString);
            var r = new EventLogReader(q);
    
            var list = new List<EventRecord>(); 
    
            EventRecord er = r.ReadEvent();
            while (er != null) {
                list.Add(er);
                er = r.ReadEvent();
            }
    

    The filter is XPath and XQuery. If you want to learn about an events internal structure I found it best to read through the filter definition within eventvwr. Look into the XML-tab...

    0 讨论(0)
提交回复
热议问题