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);//在控件中显示日志内容
                }
            }
        }

 

 

 

转载于:https://www.cnblogs.com/chenyongblog/p/3258244.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值