如何将事件日志条目限制为一个来源?

发布于 2024-09-07 10:56:13 字数 2100 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧城空念 2024-09-14 10:56:35

我从未尝试过从事件日志中读取内容,因此不确定它如何过滤它们,但建议是,您可以创建自己的日志,而不是将事件写入 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 called SSD or something and then when you write/read it it'll only be your events in there.

送舟行 2024-09-14 10:56:32

EventLog.Source 成员不能用作过滤器。根据 EventLog 的 MSDN 文档

要读取日志,请指定日志
名称和机器名称(服务器计算机
名称)的事件日志。它不是
有必要指定来源,作为
仅当写入时才需要源
日志
。条目成员是
自动填充事件
日志的条目列表。

由于您没有为 EventLog 实例的 Log 成员指定字符串,因此它会获取所有内容。

在我的脑海中,有几种方法可以解决这个问题。

首先,修改 buildListItem() 以过滤源名称。这相对简单。

其次,创建自己的日志。不要记录到 Application 日志,而是专门为您的应用程序创建一个日志。您可以通过更改构造函数来完成此操作:

public Logger(ListView _listViewControl = null, string _logFileName = null)   
{   
    if (!EventLog.SourceExists("SSD1"))   
        EventLog.CreateEventSource("SSD1", "TomWrightApplication");   
    eventLog = new EventLog("TomWrightApplication", ".", "SSD1");
    addListView(_listViewControl);   
    logFilename = _logFileName;   
}   

所有日志记录现在都将转到 TomWrightApplication 日志,而不是通用 Application 日志。


Tom,我有一个测试项目,只需执行以下操作:

static void Main()
{
    if (!EventLog.SourceExists("SSD1"))
        EventLog.CreateEventSource("SSD1", "SSDAppLog");
    EventLog log = new EventLog("SSDAppLog", ".", "SSD1");
    log.WriteEntry("this is a test");
}

除非...SSD1 源名称已在另一个日志中注册,否则该项目会成功运行。据我了解,源名称在所有事件日志中必须是唯一的。因此,如果您已经向应用程序日志注册了 SSD1,则创建新的 EventLog 时上述代码将失败。尝试使用 EventLog.DeleteEventSource() 从应用程序日志中删除 SSD1 源名称(只需为您的系统运行一次)。上面的代码应该可以工作(假设您有管理员权限)。

The EventLog.Source member doesn't work as a filter. According to the MSDN documentation for EventLog

To read from a log, specify the Log
name and MachineName (server computer
name) for the EventLog. It is not
necessary to specify the Source, as a
source is required only for writing to
logs
. The Entries member is
automatically populated with the event
log's list of entries.

Because you do not specify a string for the Log member of the EventLog 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:

public Logger(ListView _listViewControl = null, string _logFileName = null)   
{   
    if (!EventLog.SourceExists("SSD1"))   
        EventLog.CreateEventSource("SSD1", "TomWrightApplication");   
    eventLog = new EventLog("TomWrightApplication", ".", "SSD1");
    addListView(_listViewControl);   
    logFilename = _logFileName;   
}   

All logging will now go to the TomWrightApplication log, not the generic Application log.


Tom, I've got a test project that simply does the following:

static void Main()
{
    if (!EventLog.SourceExists("SSD1"))
        EventLog.CreateEventSource("SSD1", "SSDAppLog");
    EventLog log = new EventLog("SSDAppLog", ".", "SSD1");
    log.WriteEntry("this is a test");
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文