EDN Admin
Well-known member
Hi,
I am trying to read events that were logged by another application of mine.
This is the code I have so far:
<div style="color:black; background-color:white
<pre> <span style="color:blue private <span style="color:blue void ReadEventsFor(ListView listView, <span style="color:blue string eventType)
{
<span style="color:blue string eventLevel = <span style="color:#a31515 "";
<span style="color:blue switch (eventType)
{
<span style="color:blue case <span style="color:#a31515 "Information": eventLevel = <span style="color:#a31515 "4"; <span style="color:blue break;
<span style="color:blue case <span style="color:#a31515 "Warning": eventLevel = <span style="color:#a31515 "3"; <span style="color:blue break;
<span style="color:blue case <span style="color:#a31515 "Error": eventLevel = <span style="color:#a31515 "2"; <span style="color:blue break;
}
String queryString = <span style="color:blue string.Format(<span style="color:#a31515 "*[System/Level={0}][System/Provider/Name="{1}"]",
eventLevel,
<span style="color:#a31515 "MyAppName");
EventLogQuery eventsQuery = <span style="color:blue new EventLogQuery(<span style="color:#a31515 "Application", PathType.LogName, queryString);
EventLogReader logReader;
<span style="color:blue try
{
<span style="color:green // Query the log
logReader = <span style="color:blue new EventLogReader(eventsQuery);
<span style="color:green // Array of strings containing XPath references
String[] xPathRefs = <span style="color:blue new String[2];
xPathRefs[0] = <span style="color:#a31515 "Event/System/EventRecordID";
xPathRefs[1] = <span style="color:#a31515 "Event/System/TimeCreated/@SystemTime";
<span style="color:green // Place those strings in an IEnumberable object
IEnumerable<String> xPathEnum = xPathRefs;
<span style="color:green // Create the property selection context using the XPath reference
EventLogPropertySelector logPropertyContext = <span style="color:blue new EventLogPropertySelector(xPathEnum);
<span style="color:green // For each event returned from the query
<span style="color:blue for (EventRecord eventInstance = logReader.ReadEvent(); eventInstance != <span style="color:blue null; eventInstance = logReader.ReadEvent())
{
IList<<span style="color:blue object> logEventProps;
<span style="color:blue try
{
logEventProps = ((EventLogRecord)eventInstance).GetPropertyValues(logPropertyContext);
listView.Items.Add(<span style="color:blue new {
RecordID = logEventProps[0],
DateAndTime = logEventProps[1],
EventDetails = eventInstance.FormatDescription()
});
}
<span style="color:blue catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
<span style="color:blue catch (EventLogNotFoundException e)
{
MessageBox.Show(e.Message);
}
}
[/code]
This is how i use the above method:
<div style="color:black; background-color:white
<pre>ReadEventsFor(InformationListView, <span style="color:#a31515 "Information");
[/code]
When I executed this log viewer application, I am not getting any results.
Whats the correct EventLogQuery syntax to query for specific event level & provider name?
Also, is there a way to limit the results by 100 for example?
<br/>
View the full article
I am trying to read events that were logged by another application of mine.
This is the code I have so far:
<div style="color:black; background-color:white
<pre> <span style="color:blue private <span style="color:blue void ReadEventsFor(ListView listView, <span style="color:blue string eventType)
{
<span style="color:blue string eventLevel = <span style="color:#a31515 "";
<span style="color:blue switch (eventType)
{
<span style="color:blue case <span style="color:#a31515 "Information": eventLevel = <span style="color:#a31515 "4"; <span style="color:blue break;
<span style="color:blue case <span style="color:#a31515 "Warning": eventLevel = <span style="color:#a31515 "3"; <span style="color:blue break;
<span style="color:blue case <span style="color:#a31515 "Error": eventLevel = <span style="color:#a31515 "2"; <span style="color:blue break;
}
String queryString = <span style="color:blue string.Format(<span style="color:#a31515 "*[System/Level={0}][System/Provider/Name="{1}"]",
eventLevel,
<span style="color:#a31515 "MyAppName");
EventLogQuery eventsQuery = <span style="color:blue new EventLogQuery(<span style="color:#a31515 "Application", PathType.LogName, queryString);
EventLogReader logReader;
<span style="color:blue try
{
<span style="color:green // Query the log
logReader = <span style="color:blue new EventLogReader(eventsQuery);
<span style="color:green // Array of strings containing XPath references
String[] xPathRefs = <span style="color:blue new String[2];
xPathRefs[0] = <span style="color:#a31515 "Event/System/EventRecordID";
xPathRefs[1] = <span style="color:#a31515 "Event/System/TimeCreated/@SystemTime";
<span style="color:green // Place those strings in an IEnumberable object
IEnumerable<String> xPathEnum = xPathRefs;
<span style="color:green // Create the property selection context using the XPath reference
EventLogPropertySelector logPropertyContext = <span style="color:blue new EventLogPropertySelector(xPathEnum);
<span style="color:green // For each event returned from the query
<span style="color:blue for (EventRecord eventInstance = logReader.ReadEvent(); eventInstance != <span style="color:blue null; eventInstance = logReader.ReadEvent())
{
IList<<span style="color:blue object> logEventProps;
<span style="color:blue try
{
logEventProps = ((EventLogRecord)eventInstance).GetPropertyValues(logPropertyContext);
listView.Items.Add(<span style="color:blue new {
RecordID = logEventProps[0],
DateAndTime = logEventProps[1],
EventDetails = eventInstance.FormatDescription()
});
}
<span style="color:blue catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
<span style="color:blue catch (EventLogNotFoundException e)
{
MessageBox.Show(e.Message);
}
}
[/code]
This is how i use the above method:
<div style="color:black; background-color:white
<pre>ReadEventsFor(InformationListView, <span style="color:#a31515 "Information");
[/code]
When I executed this log viewer application, I am not getting any results.
Whats the correct EventLogQuery syntax to query for specific event level & provider name?
Also, is there a way to limit the results by 100 for example?
<br/>
View the full article