EventLogQuery: How to form query string?

让人想犯罪 __ 提交于 2019-12-22 04:34:13

问题


I have the following code:

string query = "???";

EventLogQuery elq = new EventLogQuery("Application", PathType.LogName, query);
elq.Session = new EventLogSession("x.x.x.x");
EventLogReader elr = new EventLogReader(elq);

I'm trying to figure out what I need to set query to in order to look for all entries with a source of "SQLSERVERAGENT".


回答1:


I have just spent an hour trying to solve similar for myself and thought I would contribute back with the solution for anyone else that comes this way. The comments should be fairly self explanatory.

  public void ReadSqlAgentEventMessages()
        {
            // Force culture to en-US if required, some people get a null from FormatDescription() and this appently solves it. 
            // My culture is set as en-GB and I did not have the issue, so I have left it as a comment to possibly ease someone's pain!
            // Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            EventLogQuery eventlogQuery = new EventLogQuery("Application", PathType.LogName, "*[System/Provider/@Name=\"SQLSERVERAGENT\"]");
            EventLogReader eventlogReader = new EventLogReader(eventlogQuery);

            // Loop through the events returned
            for (EventRecord eventRecord = eventlogReader.ReadEvent(); null != eventRecord; eventRecord = eventlogReader.ReadEvent())
            {
                // Get the description from the eventrecord. 
                string message = eventRecord.FormatDescription();

                // Do something cool with it :) 
            }
        }


来源:https://stackoverflow.com/questions/12380189/eventlogquery-how-to-form-query-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!