首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图将事件日志列表输入到在dataGridView ()方法上获得错误的事件日志

试图将事件日志列表输入到在dataGridView ()方法上获得错误的事件日志
EN

Stack Overflow用户
提问于 2017-02-15 22:33:48
回答 1查看 486关注 0票数 0

我需要在Windows中将事件日志( public static List<EventLogEntry> _LogEntries { get; private set; } )列表输入到dataGridView中。

当前问题:每次我调用方法ReadEventLog()时,它都会用异常打破未处理的'System.ArgumentException‘类型异常,出现在System.dll中的EventLog eventLog = new EventLog(EvlLocation);行中。

我先打开文件

代码语言:javascript
复制
    // Open the log file
    private void OpenFile()
    {
        string evlLocation = "";
        // Show file open dialog
        if (openFile.ShowDialog() == DialogResult.OK)
        {
            // Create a dataset for binding the data to the grid.
            ds = new DataSet("EventLog Entries");
            ds.Tables.Add("Events");
            ds.Tables["Events"].Columns.Add("ComputerName");
            ds.Tables["Events"].Columns.Add("EventId");
            ds.Tables["Events"].Columns.Add("EventType");
            ds.Tables["Events"].Columns.Add("SourceName");
            ds.Tables["Events"].Columns.Add("Message");
            // Start the processing as a background process
            evlLocation = openFile.FileName;
            parser.setLogLocation(openFile.FileName);
            worker.RunWorkerAsync(openFile.FileName);
        }
    }

//然后调用以下方法。

代码语言:javascript
复制
    // Bind the dataset to the grid.
    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        parser.ReadEventLog();
        bs = new BindingSource(ds, "Events");
        Foo foo1 = new Foo("TEST PC");
        ComputerName.Add(foo1);

        bs.DataSource = parser._LogEntries;
        //Bind fooList to the dataGridView
        dataGridView1.DataSource = bs;

        this.Invoke(pbHandler, new object[] { 100, 100 });
    }

然后,当调用ReadEventLog()时,它会在下面的EventLog eventLog = new EventLog(EvlLocation); ReadEventLog()方法行中中断

代码语言:javascript
复制
    public static void ReadEventLog()
    {
        // Line in question below
        EventLog eventLog = new EventLog(EvlLocation);
        EventLogEntryCollection eventLogEntries = eventLog.Entries;
        int eventLogEntryCount = eventLogEntries.Count;
        for (int i = 0; i < eventLogEntries.Count; i++)
        {
            EventLogEntry entry = eventLog.Entries[i];
            //Do Some processing on the entry
        }
        _LogEntries = eventLogEntries.Cast<EventLogEntry>().ToList();
    }
EN

回答 1

Stack Overflow用户

发布于 2017-02-16 14:37:14

EventLog构造器(字符串) - public EventLog(string logName)的MSDN文档中,我们在异常下面阅读。

ArgumentException _日志名称无效。

ReadEventLog()中,使用名为EvlLocation的参数构造EventLog。但是,在您所显示的代码中,没有任何地方可以初始化该属性。相反,在OpenFile()中初始化一个局部变量:

代码语言:javascript
复制
string evlLocation = "";
// ...
evlLocation = openFile.FileName;
  1. 确保初始化EvlLocation
  2. 如果错误仍然存在,验证从openFile.FileName传递的字符串是有效的-因为构造函数似乎不一致。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42261369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档