私は自分自身で同様の解決に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\"]");
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();
}
}
}