获取应用程序的日志信息

时间:2022-04-06 19:51:00

实现效果:

  获取应用程序的日志信息

知识运用:

  EventLog类的Log属性  Entries属性  

  EventLogEntryCollection类的Count属性 及 EventLogEntry类的相关属性

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            this.eventLog1.Log = "Application";
            EventLogEntryCollection collection = eventLog1.Entries;
            string info = "显示应用程序日志:" + collection.Count + "个事件";
            foreach (EventLogEntry entry in collection)                          //遍历获取到的日志
            {
                info += "\n\n 类型:" + entry.EntryType;
                info += "\n\n 日期:" + entry.TimeGenerated.ToLongDateString();
                info += "\n\n 时间:" + entry.TimeGenerated.ToLongTimeString();
                info += "\n\n 来源:" + entry.Source;
                info += "\n\n 事件:" + entry.EventID.ToString();
                info += "\n\n 用户:" + entry.UserName;
                info += "\n\n计算机:" + entry.MachineName;
            }
            richTextBox1.Text = info;
        }