如何将事件日志条目限制为一个来源?
我正在使用 EventLog 来支持 C# 应用程序中的日志记录类。 (以前...)这是该类的精简副本:
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 会填充整个日志 - 而不仅仅是来自指定源的日志。以下是输出的屏幕截图:
来自所有来源的条目 http://img341.imageshack.us /img341/6185/entriesfromalllogs.png
(在此图像中,每个条目的来源都在消息后面的括号中。)
如何让 EventLog 仅返回我的条目中的那些条目来源?我是否完全误解了EventLog?
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:
Entries from all sources http://img341.imageshack.us/img341/6185/entriesfromalllogs.png
(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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从未尝试过从事件日志中读取内容,因此不确定它如何过滤它们,但建议是,您可以创建自己的日志,而不是将事件写入
Application
日志名为SSD
或其他内容的新日志,然后当您写入/读取它时,它只会是您的事件。I've never tried reading from the event log so not sure how it works with filtering them, but a suggestion would be that instead of writing your events to the
Application
log you could create your own log create a new log calledSSD
or something and then when you write/read it it'll only be your events in there.EventLog.Source
成员不能用作过滤器。根据EventLog
的 MSDN 文档由于您没有为
EventLog
实例的Log
成员指定字符串,因此它会获取所有内容。在我的脑海中,有几种方法可以解决这个问题。
首先,修改
buildListItem()
以过滤源名称。这相对简单。其次,创建自己的日志。不要记录到
Application
日志,而是专门为您的应用程序创建一个日志。您可以通过更改构造函数来完成此操作:所有日志记录现在都将转到
TomWrightApplication
日志,而不是通用Application
日志。Tom,我有一个测试项目,只需执行以下操作:
除非...SSD1 源名称已在另一个日志中注册,否则该项目会成功运行。据我了解,源名称在所有事件日志中必须是唯一的。因此,如果您已经向应用程序日志注册了 SSD1,则创建新的 EventLog 时上述代码将失败。尝试使用
EventLog.DeleteEventSource()
从应用程序日志中删除 SSD1 源名称(只需为您的系统运行一次)。上面的代码应该可以工作(假设您有管理员权限)。The
EventLog.Source
member doesn't work as a filter. According to the MSDN documentation forEventLog
Because you do not specify a string for the
Log
member of theEventLog
instance, it is getting everything.Off the top of my head, there are a couple of ways to deal with this.
First, modify
buildListItem()
to filter on your source name. This is relatively straightforward.Second, create your own log. Instead of logging to the
Application
log, create a log specifically for your application. You can do this by altering your constructor:All logging will now go to the
TomWrightApplication
log, not the genericApplication
log.Tom, I've got a test project that simply does the following:
This works successfully unless...the SSD1 source name is already registered with another log. As I understand it, the source name must be unique across all event logs. So, if you've already registered SSD1 with the Application log, the above code will fail when creating the new EventLog. Try using the
EventLog.DeleteEventSource()
to remove the SSD1 source name from the Application log (just run this one time for your system). The above code should work then (assuming you have admin privileges).