I just spent an hour trying to solve this for myself, and thought that I would be returning to a solution for everyone who comes here. Comments should be sufficiently 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 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 :) } }
Murray foxcroft
source share