2017-02-15 3 views
0

Windowsフォームのpublic static List<EventLogEntry> _LogEntries { get; private set; }dataGridViewというイベントログのリストを取得する必要があります。イベントログのリストをdataGridViewに取得しようとしています。 ReadEventLog()メソッドでエラーが発生する

最新号:毎回私は型「System.ArgumentExceptionの」の未処理の例外がラインEventLog eventLog = new EventLog(EvlLocation);


でSystem.dllので発生し、それは例外を破るReadEventLog()メソッドを呼び出します最初に私はファイルを開きます

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

//次に、以下のmetho dが呼び出されます。 public EventLog(string logName) - - 呼び出されたときにReadEventLog()

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

は、その後、それはEventLog eventLog = new EventLog(EvlLocation); ReadEventLog()方法EventLog Constructor (String)のためのMSDNのドキュメントで

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

下の行に分割し、我々は例外の下で読ん

ArgumentException |ログ名が無効です。 ReadEventLog()

あなたはEvlLocationという名前のパラメータを使用してEventLogを構築します。しかし、あなたが示したコードのどこにも、そのプロパティを初期化していません。その代わり、OpenFile()には、ローカル変数を初期化します。

string evlLocation = ""; 
// ... 
evlLocation = openFile.FileName; 
  1. あなたがEvlLocationを初期化することを確認してください。
  2. エラーが解決しない場合は、openFile.FileNameから渡された文字列が有効であることを確認してください。コンストラクタは同意していないようです。
関連する問題