试图将事件日志列表放入dataGridView中在ReadEventLog()方法

时间:2017-02-15 22:33:48

标签: c# winforms datagridview event-log

我需要在Windows窗体中将此事件日志列表public static List<EventLogEntry> _LogEntries { get; private set; }放入dataGridView

  

当前问题:每当我调用方法ReadEventLog()时,它都会以异常在System {<1>}的System.dll 中发生未处理的“System.ArgumentException”类型的异常异常}

首先我打开文件

EventLog eventLog = new EventLog(EvlLocation);

//然后调用以下方法。

    // 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);
        }
    }

然后当 // 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()

1 个答案:

答案 0 :(得分:0)

EventLog Constructor (String) - public EventLog(string logName)的MSDN文档中,我们在例外下阅读:

  

ArgumentException |日志名称无效。

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

string evlLocation = "";
// ...
evlLocation = openFile.FileName;
  1. 确保您初始化EvlLocation
  2. 如果错误仍然存​​在,请验证从openFile.FileName传递的字符串是否有效 - 因为构造函数似乎不同意。