zoukankan      html  css  js  c++  java
  • 使用EventLog组件保存Windows系统日志

    实现效果:

      

    知识运用:

      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("系统没有错误日志"); }
            }
    
  • 相关阅读:
    《梦断代码》随笔第1篇
    四则运算3
    1、软件工程结对开发之求一维数组中连续最大子数组之和
    四则运算2单元测试
    《梦断代码》随笔第0篇
    四则运算2完整版
    四则运算2设计思想
    软件工程第一个程序
    软件工程阅读计划
    电梯调度之需求分析
  • 原文地址:https://www.cnblogs.com/feiyucha/p/10191775.html
Copyright © 2011-2022 走看看