I'm using EventLog to support a logging class in my C# application. (Previously...) Here's a whittled down copy of that class:
class Logger
{
private EventLog eventLog;
private ListView listViewControl = null;
private String logSource = "SSD1";
public Logger(ListView _listViewControl = null, string _logFileName = null)
{
if (!EventLog.SourceExists("SSD1"))
EventLog.CreateEventSource(logSource, "Application");
eventLog = new EventLog();
eventLog.Source = logSource;
addListView(_listViewControl);
logFilename = _logFileName;
}
public void addListView(ListView newListView)
{
if (eventLog.Entries.Count > 0)
{
foreach (EventLogEntry entry in eventLog.Entries)
{
listViewControl.Items.Add(buildListItem(entry));
}
}
}
public void LogInformation(string message)
{
LogEntry(message, EventLogEntryType.Information);
}
private void LogEntry(string message, EventLogEntryType logType)
{
eventLog.WriteEntry(message, logType);
if (listViewControl != null)
{
updateListView();
}
}
private void updateListView()
{
listViewControl.Items.Add(buildListItem(eventLog.Entries[eventLog.Entries.Count-1]));
}
private ListViewItem buildListItem(EventLogEntry entry)
{
string[] eventArray = new string[3];
eventArray[0] = entry.Message + " (" + entry.Source +")";
eventArray[1] = entry.EntryType.ToString();
eventArray[2] = entry.TimeGenerated.ToString("dd/MM/yyyy - HH:mm:ss");
return new ListViewItem(eventArray);
}
The problem is, the ListView gets populated with the whole of the log - not just those from the specified source. Here's a screenshot of the output:
(In this image, the source of each entry is in brackets after the message.)
How do I get the EventLog to return only those entries from my source? Have I misunderstood EventLog completely?