我需要在Windows窗體中將事件日誌public static List<EventLogEntry> _LogEntries { get; private set; }
的列表變成dataGridView
。嘗試將事件日誌列表導入到dataGridView中|在ReadEventLog()方法上得到錯誤
當前問題:每次我打電話
ReadEventLog()
它異常類型「System.ArgumentException」未處理的異常打破了方法在System.dll中發生在行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);
}
}
//然後下面的方法d被稱爲。
// 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()
方法如下
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();
}