2017-02-15 1 views
0

Мне нужно получить этот список журналов событий public static List<EventLogEntry> _LogEntries { get; private set; } в dataGridView в Windows Forms.Пытается получить список журналов событий в dataGridView | получение ошибки в методе ReadEventLog()

Текущий выпуск: Everytime я называю метод 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() вы строите EventLog, используя параметр с именем EvlLocation. Но нигде в коде, который вы показали, вы инициализируете это свойство. Вместо этого, в OpenFile() инициализации локальной переменной:

string evlLocation = ""; 
// ... 
evlLocation = openFile.FileName; 
  1. Убедитесь, что вы инициализации EvlLocation.
  2. Если ошибка повторяется, проверьте, что строка, переданная с openFile.FileName, действительна - поскольку конструктор, похоже, не согласен.
Смежные вопросы