Fork me on GitHub

EventLog组件

1、使用EventLog组件读写事件日志

SourceExists方法  确定事件源是否已在本地计算机上注册

DeleteEventSource方法  用于从事件日志中移除应用程序的事件源注册

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            if (eventLog1.Entries.Count > 0)
            {
                foreach (System.Diagnostics.EventLogEntry entry
                   in eventLog1.Entries)
                {
                    listBox1.Items.Add(entry.Message);
                }
            }
            else
            {
                MessageBox.Show("日志中没有记录.");
            }
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            if (System.Diagnostics.EventLog.SourceExists("ZhyScoure"))//判断是否存在事件源
            {
                System.Diagnostics.EventLog.DeleteEventSource("ZhyScoure");//删除事件源注册
            }
            System.Diagnostics.EventLog.//创建日志信息
                CreateEventSource("ZhyScoure", "NewLog1");
            eventLog1.Log = "NewLog1";//设置日志名称
            eventLog1.Source = "ZhyScoure";//事件源名称
            this.eventLog1.MachineName = ".";//表示本机
        }

        private void btn_Write_Click(object sender, EventArgs e)
        {
            if (System.Diagnostics.EventLog.Exists("NewLog1"))//判断日志是否存在
            {
                if (textBox1.Text != "")//如果文本框为空
                {
                    eventLog1.WriteEntry(textBox1.Text.ToString());//写入日志
                    MessageBox.Show("日志写成功");//弹出消息对话框
                    textBox1.Text = "";//清空文本框信息
                }
                else
                {
                    MessageBox.Show("日志内容不能为空");//弹出消息对话框
                }
            }
            else
            {
                MessageBox.Show("日志不存在");//弹出消息对话框
            }
        }

保存Windows系统日志

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            if (System.Diagnostics.EventLog.SourceExists("ErrEventLog"))//判断是否存在事件源
            {
                System.Diagnostics.EventLog.DeleteEventSource("ErrEventLog");//删除事件源注册
            }
            System.Diagnostics.EventLog.//创建日志信息
                CreateEventSource("ErrEventLog", "Application");
            eventLog2.Log = "Application";//设置日志名称
            eventLog2.Source = "ErrEventLog";//事件源名称
            this.eventLog1.MachineName = ".";//表示本机
        }
        private void btn_Find_Click(object sender, EventArgs e)
        {
            if (eventLog1.Entries.Count > 0)//判断是否存在系统日志
            {
                foreach (System.Diagnostics.EventLogEntry//遍历日志信息
                    entry in eventLog1.Entries)
                {
                    if (entry.EntryType ==//判断是否为错误日志
                        System.Diagnostics.EventLogEntryType.Error)
                    {
                        listBox1.Items.Add(entry.Message);//向控件中添加数据项
                        eventLog2.WriteEntry(entry.Message,//写入日志信息
                            System.Diagnostics.EventLogEntryType.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("系统没有错误日志.");//弹出消息对话框
            }
        }

向本机现有日志中添加条目

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            if (eventLog1.Entries.Count > 0)
            {
                foreach (System.Diagnostics.EventLogEntry//遍历所有日志
                    entry in eventLog1.Entries)
                {
                    if (comboBox1.Items.Count == 0)//判断是否为第一个日志
                    {
                        comboBox1.Items.Add(//添加日志信息
                            entry.Source.ToString());
                    }
                    else
                    {
                        if (!comboBox1.Items.Contains(//判断产生日志信息的应用程序是否重复
                            entry.Source.ToString()))
                        {
                            comboBox1.Items.Add(//添加日志信息
                                entry.Source.ToString());
                        }
                    }
                }
            }
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem == null)//如果没有选择应用程序
            {
                MessageBox.Show("请选择日志名称");//弹出消息对话框
                return;
            }
            if (textBox1.Text == "")//如果没有添写日志内容
            {
                MessageBox.Show("请填写日志内容");//弹出消息对话框
                textBox1.Focus();//控件得到焦点
                return;//退出方法
            }
            eventLog1.Log = "System";//设置读写日志的名称
            eventLog1.Source = comboBox1.//设置日志源名称
                SelectedItem.ToString();
            eventLog1.MachineName = ".";//设置写入日志的计算机名称
            eventLog1.WriteEntry(textBox1.Text);
            MessageBox.Show("添加成功");//弹出提示信息
            if (eventLog1.Entries.Count > 0)//如果日志中有内容
            {
                foreach (System.Diagnostics.EventLogEntry//遍历日志内容
                    entry in eventLog1.Entries)
                {
                    listView1.Items.Add(entry.Message);//在控件中显示日志内容
                }
            }
        }

 

 

 

posted @ 2013-08-14 19:24  种花生的读书人  阅读(431)  评论(0编辑  收藏  举报

该博客仅作为记录笔记,转载随意