2017-02-14 2 views
0

이벤트 로그 파일을 dataGridView (Windows Forms)으로 구문 분석하려고합니다. EventLogEntries public static List<EventLogEntry> _LogEntries { get; private set; }의 목록을 그리드보기에 넣어야합니다. 나는 dataGridView가 작동하지만 listBox가 작동 할 수도 있다고 생각합니다.목록의 데이터를 Windows의 그리드보기로 가져 오기 양식

내 목록 _LogEntries의 데이터를 Windows 양식의 그리드보기로 가져와야합니다. 내가 어떻게 그럴 수 있니? 다음은

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

파서 클래스의 MainForm.cs의 코드이지만, 지금은 때를 ReadEventLog()을 호출하면`System.ArgumentException '유형의 예외가 System.dll에서 발생하지만 그것이 가지고 만들 것 있도록

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

당신은 정적 생성자 정적 속성을 초기화 할 수 있습니다 : 당신은 적절한 방법으로 parser 클래스의 정적 _LogEntries 변수를 초기화해야

+0

이것을 실행 한 적이 있습니까? 오류가 있습니까? 그렇다면 무엇입니까? – Rinktacular

+0

필자는이 파일을 실행 시켰습니다. 'bs.DataSource = parser._LogEntries;'유형이 'System.TypeInitializationException'인 경우 예외입니다. –

+0

여기 새로 왔으므로 나중에 문제가 발생하지 않습니다. 귀하의 질문에 그 오류를 포함하려면, 그것은 사람들이 대답하는 데 도움이됩니다 :) 어쨌든, 나는 결코 '파서'는 어디서나 정의되는 것을 보지 못합니다. 문제의 그 부분입니까? – Rinktacular

답변

0

사용자 코드에서 처리되지 않았습니다 사용 전에 초기화되었습니다.

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

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

코드 하단에 있습니다. –

+0

정확히 어디에서 초기화했는지 모르겠습니다. –

+0

파서 클래스 상단 –

관련 문제