【发布时间】:2010-06-23 16:49:23
【问题描述】:
我正在使用 EventLog 来支持我的 C# 应用程序中的日志记录类。 (Previously...) 这是该课程的精简版:
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);
}
问题是,ListView 会填充整个日志 - 而不仅仅是来自指定来源的日志。这是输出的屏幕截图:
Entries from all sources http://img341.imageshack.us/img341/6185/entriesfromalllogs.png
(在此图像中,每个条目的来源都在消息后面的括号中。)
我如何让 EventLog 从我的源返回只那些条目?我是否完全误解了 EventLog?
【问题讨论】: