2017-02-14 50 views
0

我想解析事件日誌文件到dataGridViewWindows Forms。 我需要將EventLogEntry public static List<EventLogEntry> _LogEntries { get; private set; }的列表放入網格視圖中。 我相信dataGridView會工作,但列表框也可能。從列表中獲取數據到窗體中的網格視圖

我需要從我的列表_LogEntries獲取數據到Windows窗體網格視圖。我會怎麼做?

下面是從下面MainForm.cs

public static class parser 
{ 
    public static string EvlLocation { get; set; } 
    public static string evlLocationManual = "K:\\Event Log\\Test\\Test.evt"; 
    public static List<EventLogEntry> _LogEntries { get; private set; } 

    static parser() 
    { 
     _LogEntries = new List<EventLogEntry>(); 
    } 

    public static void ReadEventLog() 
    { 
     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(); 
    } 

    public static void ParseTest() 
    { 
     evlLocationManual = "K:\\Event Log\\Test\\Test.evt"; 
     ReadEventLog(); 
    } 

    public static void setLogLocation(string input) 
    { 
     EvlLocation = input; 
    } 
} 

public static class EventLogEntryCollection_Container 
{ 
    public static void testCollection() 
    { 
     string myLogName = "_log"; 

     // Create an EventLog instance and assign its source. 
     EventLog _log = new EventLog(); 
     _log.Source = "%Program Files (x86)%\\EventLogParser\\ImportedEventLogs\\" + varBank.logInput; 

     // Write an informational entry to the event log. 
     _log.WriteEntry("Successfully created a new Entry in the Log"); 
     _log.Close(); 

     // Create a new EventLog object. 
     EventLog myEventLog1 = new EventLog(); 
     myEventLog1.Log = myLogName; 

     // Obtain the Log Entries of "_log". 
     EventLogEntryCollection _logCollection = _log.Entries; 
     _log.Close(); 

     // Copy the EventLog entries to Array of type EventLogEntry. 
     EventLogEntry[] _logEntryArray = new EventLogEntry[_logCollection.Count]; 
     _logCollection.CopyTo(_logEntryArray, 0); 
     IEnumerator myEnumerator = _logEntryArray.GetEnumerator(); 
     while (myEnumerator.MoveNext()) 
     { 
      EventLogEntry myEventLogEntry = (EventLogEntry)myEnumerator.Current; 
     } 
    } 
} 

我能夠修正一些錯誤的代碼從EventLogParser.cs Windows Forms

private List<Foo> ComputerName = new List<Foo>(); 
    private List<Foo> EventId = new List<Foo>(); 
    private List<Foo> EventType = new List<Foo>(); 
    private List<Foo> SourceName = new List<Foo>(); 
    private List<Foo> Message = new List<Foo>(); 

    class Foo : INotifyPropertyChanged 
    { 
     private string bar_; 
     public string Bar 
     { 
      get { return bar_; } 
      set 
      { 
       bar_ = value; 
       NotifyPropertyChanged("Bar"); 
      } 
     } 

     public Foo(string bar) 
     { 
      this.Bar = bar; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(string info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

     public override string ToString() 
     { 
      return bar_; 
     } 
    } 

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     var bs = new BindingSource(ds, "Events"); 
     Foo foo1 = new Foo("TEST PC"); 
     ComputerName.Add(foo1); 

     parser.ReadEventLog(); 
     bs.DataSource = parser._LogEntries; 
     //Bind fooList to the dataGridView 
     dataGridView1.DataSource = bs; 
     //I can see bar1 in the listbox as expected 

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

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

解析器類,但現在當我調用ReadEventLog()它拋出`類型System.ArgumentException異常'在System.dll中發生但

public static List<EventLogEntry> _LogEntries = new List<EventLogEntry>(); 

可以初始化靜態構造函數的靜態屬性,以便它會使它具有:在用戶代碼

+0

你跑了嗎?你有錯誤嗎?如果是這樣,他們是什麼? – Rinktacular

+0

我已經運行它,它在線'bs.DataSource = parser._LogEntries;'中斷了'類型'System.TypeInitializationException'的異常 –

+0

你在這裏是新的,所以這不是一個問題,但在未來,你會想要在你的問題中包括這些錯誤,它可以幫助人們回答:) 無論如何,我從來沒有看到'parser'在任何地方被定義..是問題的一部分嗎? – Rinktacular

回答

0

你應該初始化parser類的靜態_LogEntries變量在適當的方式沒有處理在使用之前已經初始化。

public static List<EventLogEntry> _LogEntries { get; private set; } 

static parser() 
{ 
    _LogEntries = new List<EventLogEntry>(); 
} 
+0

我做了它在底部的代碼 –

+0

我沒有看到你究竟在哪裏初始化。 –

+0

解析器類的頂部 –

相關問題