我需要在Windows中将事件日志( public static List<EventLogEntry> _LogEntries { get; private set; } )列表输入到dataGridView中。
当前问题:每次我调用方法
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);
}
}//然后调用以下方法。
// 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();
}发布于 2017-02-16 14:37:14
在EventLog构造器(字符串) - public EventLog(string logName)的MSDN文档中,我们在异常下面阅读。
ArgumentException _日志名称无效。
在ReadEventLog()中,使用名为EvlLocation的参数构造EventLog。但是,在您所显示的代码中,没有任何地方可以初始化该属性。相反,在OpenFile()中初始化一个局部变量:
string evlLocation = "";
// ...
evlLocation = openFile.FileName;EvlLocation。openFile.FileName传递的字符串是有效的-因为构造函数似乎不一致。https://stackoverflow.com/questions/42261369
复制相似问题