gpt4 book ai didi

c# - 事件日志查询 : How to form query string?

转载 作者:太空狗 更新时间:2023-10-29 19:47:16 25 4
gpt4 key购买 nike

我有以下代码:

string query = "???";

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

我试图弄清楚我需要将查询设置为什么,以便查找源为“SQLSERVERAGENT”的所有条目。

最佳答案

我刚刚花了一个小时尝试为自己解决类似的问题,并认为我会为以这种方式出现的任何其他人提供解决方案。评论应该是相当 self 解释的。

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\"]");
using (EventLogReader eventlogReader = new EventLogReader(eventlogQuery))
{
EventRecord eventRecord = eventlogReader.ReadEvent();
try
{

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

// Do something cool with it :)
}
}
finally
{
if (eventRecord != null)
eventRecord.Dispose();
}
}
}

关于c# - 事件日志查询 : How to form query string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12380189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com