使用EventLog组件保存Windows系统日志
2023-09-14 09:00:20 时间
实现效果:
知识运用:
EventLog类的CreateEventSource方法 //用于建立一个应用程序 使用指定的Sourc作为向本机上的日志中写入日志项的有效事件源
CreateEventSource(string source,string logName) //亦可以在本机上创建一个新的自定义日志
Log属性 //获取或设置在读取或写入的日志名称
public string Log{get;set;}
Source属性 //获取或设置在写入事件日志时要注册和使用的源名称
public string Source{get;set;}
实现代码:
private void Form1_Load(object sender, EventArgs e) { if (System.Diagnostics.EventLog.SourceExists("ErrEventLog")) { System.Diagnostics.EventLog.DeleteEventSource("ErrEventLog"); } System.Diagnostics.EventLog.CreateEventSource("ErrEventLog","Application"); eventLog1.Log = "Application"; eventLog1.Source = "ErrEventLog"; eventLog1.MachineName = "."; } private void btn_find_Click(object sender, EventArgs e) { if (eventLog1.Entries.Count>0) { foreach(System.Diagnostics.EventLogEntry evn in eventLog1.Entries ) { if (evn.EntryType == System.Diagnostics.EventLogEntryType.Error) listBox1.Items.Add(evn.Message); } } else { MessageBox.Show("系统没有错误日志"); } }
相关文章
- Windows下Nginx的启动、停止等命令&Nginx 配置多域名&windows下设置Nginx开机自动启动
- 在Windows命令行中编译运行C/C++程序
- Windows代理设置
- Windows下Git多账号配置,同一电脑多个ssh-key的管理
- ubuntu 连接windows远程桌面 &&rdesktop 退出全屏模式
- MFC Windows 程序设计[六十]之窗口分屏(附源码)
- MFC Windows 程序设计[二十四]之windows目录展示(附源码)
- MFC Windows 程序设计(二)-初尝禁果
- WINDOWS.H already included. MFC apps must not #include <Windows.h>
- LabVIEW与Microsoft Windows的兼容性
- GDI+入门——带你走进Windows图形的世界
- 在Windows里生成SSL的证书和使用python进行通讯
- Windows API一日一练(70)GetSystemTime和GetLocalTime函数