【问题标题】:How to limit EventLog entries to one source?如何将 EventLog 条目限制为一个来源?
【发布时间】: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?

【问题讨论】:

    标签: c# listview event-log


    【解决方案1】:

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

    要从日志中读取,请指定日志 名称和 MachineName(服务器计算机 名称)用于事件日志。它不是 有必要指定来源,作为 source 仅用于写入 日志。参赛成员是 自动填充事件 日志的条目列表。

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

    【讨论】:

    • 嗨,马特,在尝试使用第二种方法时,我收到以下错误:“计算机上的事件日志 'SSDAppLog'。”不存在。”有什么建议吗?
    【解决方案2】:

    我从未尝试从事件日志中读取数据,因此不确定它如何过滤它们,但建议不要将事件写入 Application 日志,而是创建自己的日志创建一个新日志称为SSD 或其他东西,然后当你写/读它时,它只会是你的事件。

    【讨论】:

      猜你喜欢
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 2014-05-08
      相关资源
      最近更新 更多