EDN Admin
Well-known member
Hi,
I am trying to read off event log using the EventLogQuery & EventLogReader and for some odd reason, the eventInstance.FormatDescription() is not working, its returning empty string.
This is the code I have so far:
<pre lang="x-c# private void ReadEventsFor(ListView listView, string eventType)
{
// Clear List View
listView.Items.Clear();
// Workout Which Tab We Are Working With & Number Of Records To Display
int eventLevel = 0;
int[] eventLimit = new int[5];
switch (eventType)
{
case "Information":
{
eventLevel = 4;
eventLimit[eventLevel] = int.Parse(((ComboBoxItem)InformationLimitSelection.SelectedItem).Tag.ToString());
}
break;
case "Warning":
{
eventLevel = 3;
eventLimit[eventLevel] = int.Parse(((ComboBoxItem)WarningLimitSelection.SelectedItem).Tag.ToString());
}
break;
case "Error":
{
eventLevel = 2;
eventLimit[eventLevel] = int.Parse(((ComboBoxItem)ErrorLimitSelection.SelectedItem).Tag.ToString());
}
break;
}
// Create Event Log Query & Reader
EventLogQuery eventsQuery = new EventLogQuery("Application",
PathType.LogName,
string.Format("*[System/Level={0}][System/Provider/@Name="{1}"]",
eventLevel,
eventLogSource));
EventLogReader logReader;
// Query the log
logReader = new EventLogReader(eventsQuery);
// Array of strings containing XPath references
String[] xPathRefs = new String[1];
xPathRefs[0] = "Event/System/EventRecordID";
// Place those strings in an IEnumberable object
IEnumerable<String> xPathEnum = xPathRefs;
// Create the property selection context using the XPath reference
EventLogPropertySelector logPropertyContext = new EventLogPropertySelector(xPathEnum);
// For each event returned from the query
int eventCounter = 0;
for (EventRecord eventInstance = logReader.ReadEvent(); eventInstance != null; eventInstance = logReader.ReadEvent())
{
IList<object> logEventProps = ((EventLogRecord)eventInstance).GetPropertyValues(logPropertyContext);
listView.Items.Add(new
{
RecordID = logEventProps[0],
EventDetails = eventInstance.FormatDescription()
});
eventCounter++;
if (eventCounter == eventLimit[eventLevel]) break;
}
// Update Tab Status
string statusMessage = string.Format("Displaying {0} Records", eventCounter);
switch (eventType)
{
case "Information": InformationStatus.Content = statusMessage; break;
case "Warning": WarningStatus.Content = statusMessage; break;
case "Error": ErrorStatus.Content = statusMessage; break;
}
}[/code]
<br/>
On my application, this is how it looks like:<br/>
http://img684.imageshack.us/img684/9629/unledgkx.png" target="_blank http://img684.imageshack.us/img684/9629/unledgkx.png
http://img828.imageshack.us/img828/7146/unleddld.png" target="_blank
Any idea what I am doing wrong here? Thanks in advance for any help / tip.
View the full article
I am trying to read off event log using the EventLogQuery & EventLogReader and for some odd reason, the eventInstance.FormatDescription() is not working, its returning empty string.
This is the code I have so far:
<pre lang="x-c# private void ReadEventsFor(ListView listView, string eventType)
{
// Clear List View
listView.Items.Clear();
// Workout Which Tab We Are Working With & Number Of Records To Display
int eventLevel = 0;
int[] eventLimit = new int[5];
switch (eventType)
{
case "Information":
{
eventLevel = 4;
eventLimit[eventLevel] = int.Parse(((ComboBoxItem)InformationLimitSelection.SelectedItem).Tag.ToString());
}
break;
case "Warning":
{
eventLevel = 3;
eventLimit[eventLevel] = int.Parse(((ComboBoxItem)WarningLimitSelection.SelectedItem).Tag.ToString());
}
break;
case "Error":
{
eventLevel = 2;
eventLimit[eventLevel] = int.Parse(((ComboBoxItem)ErrorLimitSelection.SelectedItem).Tag.ToString());
}
break;
}
// Create Event Log Query & Reader
EventLogQuery eventsQuery = new EventLogQuery("Application",
PathType.LogName,
string.Format("*[System/Level={0}][System/Provider/@Name="{1}"]",
eventLevel,
eventLogSource));
EventLogReader logReader;
// Query the log
logReader = new EventLogReader(eventsQuery);
// Array of strings containing XPath references
String[] xPathRefs = new String[1];
xPathRefs[0] = "Event/System/EventRecordID";
// Place those strings in an IEnumberable object
IEnumerable<String> xPathEnum = xPathRefs;
// Create the property selection context using the XPath reference
EventLogPropertySelector logPropertyContext = new EventLogPropertySelector(xPathEnum);
// For each event returned from the query
int eventCounter = 0;
for (EventRecord eventInstance = logReader.ReadEvent(); eventInstance != null; eventInstance = logReader.ReadEvent())
{
IList<object> logEventProps = ((EventLogRecord)eventInstance).GetPropertyValues(logPropertyContext);
listView.Items.Add(new
{
RecordID = logEventProps[0],
EventDetails = eventInstance.FormatDescription()
});
eventCounter++;
if (eventCounter == eventLimit[eventLevel]) break;
}
// Update Tab Status
string statusMessage = string.Format("Displaying {0} Records", eventCounter);
switch (eventType)
{
case "Information": InformationStatus.Content = statusMessage; break;
case "Warning": WarningStatus.Content = statusMessage; break;
case "Error": ErrorStatus.Content = statusMessage; break;
}
}[/code]
<br/>
On my application, this is how it looks like:<br/>
http://img684.imageshack.us/img684/9629/unledgkx.png" target="_blank http://img684.imageshack.us/img684/9629/unledgkx.png
http://img828.imageshack.us/img828/7146/unleddld.png" target="_blank
Any idea what I am doing wrong here? Thanks in advance for any help / tip.
View the full article