Po prostu spędziłem godzinę próbując rozwiązać podobne dla siebie i myślałem, że przyczynię się z powrotem rozwiązanie dla każdego, kto przychodzi w ten sposób. Komentarze powinny być dość oczywiste.
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 :)
}
}
Jeśli używam Podglądu zdarzeń do ustawienia filtra, widzę nieprzetworzone zapytanie XML, z którego korzysta. Otrzymuję ciąg, taki jak ''. Czy którekolwiek z tej pracy (cała rzecz, bez tagu XML, lub po prostu 'Provider [@Name = '...']'? –
będzie [C#: Jak zapytać o szczegóły dziennika zdarzeń z danego identyfikatora zdarzenia? ] (http://stackoverflow.com/questions/2462426/c-how-to-query-for-an-event-log-details-with-a-given-event-id) pomaga? – Turbot
Myślę [ten post jest odpowiedź] [1] [1]. http://stackoverflow.com/a/8575390/284758 –