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\"]");
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();
}
}
}
<Select Path="Application">*[System[Provider[@Name='SourceName']]]</Select>
. Does any of this work (the whole thing, minus the XML tag, or justProvider[@Name='...']
? – Clinch