2017-02-15 1 views
0

Je dois obtenir cette liste de journaux d'événements public static List<EventLogEntry> _LogEntries { get; private set; } en dataGridView dans Windows Forms.Essayer d'obtenir la liste des journaux d'événements dans un DataGridView | erreur sur la méthode ReadEventLog()

Problème actuel: Chaque fois que j'appelle la méthode ReadEventLog() il se casse à l'exception Une exception non gérée du type 'System.ArgumentException' a eu lieu dans System.dll à la ligne EventLog eventLog = new EventLog(EvlLocation);


D'abord, j'ouvre le fichier

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

// Ensuite, la méthode ci-dessous d est appelé.

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

Puis, quand ReadEventLog() est appelé brise à la ligne EventLog eventLog = new EventLog(EvlLocation); méthode ReadEventLog() ci-dessous

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

Répondre

0

Dans la documentation MSDN pour EventLog Constructor (String)-public EventLog(string logName) - nous lisons sous exceptions :

ArgumentException | Le nom du journal est invalide.

Dans ReadEventLog() vous construisez EventLog à l'aide d'un paramètre nommé EvlLocation. Mais nulle part dans le code que vous avez montré, vous initialisez cette propriété. Au lieu de cela, dans OpenFile() vous initialisez une variable locale:

string evlLocation = ""; 
// ... 
evlLocation = openFile.FileName; 
  1. Assurez-vous initialisez EvlLocation.
  2. Si l'erreur persiste, vérifiez que la chaîne passée de openFile.FileName est valide car le constructeur ne semble pas être d'accord.