我正在尝试将事件日志文件解析为dataGridView中的Windows Forms。我需要将EventLogEntries public static List<EventLogEntry> _LogEntries { get; private set; }的列表放到网格视图中。我相信dataGridView会起作用,但listBox也可能。
我需要将
_LogEntries列表中的数据输入到windows窗体中的网格视图中。我该怎么做?
下面是MainForm.cs for 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);
}
}解析器类来自下面的EventLogParser.cs
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;
}
}
}我能够修复一些错误,但是现在当我调用
ReadEventLog()时,抛出的‘System.ArgumentException类型的异常’发生在System.dll中,但是没有在用户代码中处理
发布于 2017-02-14 22:09:36
您应该以正确的方式初始化_LogEntries类的静态parser变量:
public static List<EventLogEntry> _LogEntries = new List<EventLogEntry>();可以在静态构造函数中初始化静态属性,以便使其在使用之前已经初始化。
public static List<EventLogEntry> _LogEntries { get; private set; }
static parser()
{
_LogEntries = new List<EventLogEntry>();
}https://stackoverflow.com/questions/42236771
复制相似问题