2017-02-15 22 views
0

我需要在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(); 
    } 

回答

0

在MSDN文檔EventLog Constructor (String) - public EventLog(string logName) - 我們下例外閱讀

ArgumentException |日誌名稱無效。

ReadEventLog()您使用一個名爲EvlLocation參數構建EventLog。但是在你顯示的代碼中沒有任何地方是你初始化那個屬性。相反,在OpenFile()初始化一個局部變量:

string evlLocation = ""; 
// ... 
evlLocation = openFile.FileName; 
  1. 確保你初始化EvlLocation
  2. 如果錯誤仍然存​​在,請驗證從openFile.FileName傳遞的字符串是否有效 - 因爲構造函數似乎不同意。
相關問題