EventLogQuery:如何形成的查询字符串?(EventLogQuery: How to fo

2019-07-31 12:32发布

我有以下代码:

string query = "???";

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

我试图找出我需要查询,以寻找与“SQLSERVERAGENT”来源的所有条目设置。

Answer 1:

我刚刚花了一个小时试图解决自己相似,以为我会回来贡献与其他任何人来这样的解决方案。 这些评论应该是相当自我解释。

  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 :) 
            }
        }


文章来源: EventLogQuery: How to form query string?
标签: c# event-log