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

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

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

        private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
if (eventLog1.Entries.Count > )
{
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 > )//判断是否存在系统日志
{
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 > )
{
foreach (System.Diagnostics.EventLogEntry//遍历所有日志
entry in eventLog1.Entries)
{
if (comboBox1.Items.Count == )//判断是否为第一个日志
{
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 > )//如果日志中有内容
{
foreach (System.Diagnostics.EventLogEntry//遍历日志内容
entry in eventLog1.Entries)
{
listView1.Items.Add(entry.Message);//在控件中显示日志内容
}
}
}

EventLog组件的更多相关文章

  1. 使用EventLog组件向本机现有日志中添加条目

    实现效果: 知识运用: EventLog组件的MachineName属性 //获取或设置在其上读取或写入事件的计算机名称 public string MachineName  {get;set; } ...

  2. EventLog组件读写事件日志

    使用.Net中的EventLog控件使您可以访问或自定义Windows 事件日志,事件日志记录关于重要的软件或硬件事件的信息.通过 EventLog,可以读取现有日志,向日志中写入项,创建或删除事件源 ...

  3. 使用EventLog组件保存Windows系统日志

    实现效果: 知识运用: EventLog类的CreateEventSource方法 //用于建立一个应用程序  使用指定的Sourc作为向本机上的日志中写入日志项的有效事件源 CreateEventS ...

  4. 使用EventLog组件读写事件日志

    实现效果: 知识运用: Eventlog类的SourceExists方法 //确定指定的事件源是否已在本地计算机注册 public static bool  SourceExists(string s ...

  5. 一文了解如何源码编译Rainbond基础组件

    Rainbond 主要由以下三个项目组成,参考官网详细 技术架构 业务端 Rainbond-UI 和 Rainbond-Console 合起来构成了业务层.业务层是前后端分离模式.UI是业务层的前端代 ...

  6. 如何用.NET创建Windows服务

    我们将研究如何创建一个作为Windows服务的应用程序.内容包含什么是Windows服务,如何创建.安装和调试它们.会用到System.ServiceProcess.ServiceBase命名空间的类 ...

  7. 服务器重启后SQL Server Agent由于"The EventLog service has not been started" 启动失败

    案例环境: 操作系统   : Microsoft Windows Server 2003 Standard Edtion SP2 数据库版本 : SQL Server 2005 Standard Ed ...

  8. web 前端常用组件【01】Pagination 分页

    分页组件几乎是一般网站都会涉及到的组件,网上有很多这样的插件,自己挑来跳去选择了这一款. 官方Demo网址:http://mricle.com/JqueryPagination 功能强大,可扩展性比较 ...

  9. 添加无线服务wzcsvc服务,Eventlog服务

    <添加eventlog服务.reg> Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentCont ...

随机推荐

  1. 【转】cocos2d-x 2.0版本 自适应屏幕分辨率

    http://codingnow.cn/cocos2d-x/975.html 我使用的版本是cocos2d-2.0-x-2.0.4,cocos2dx-2.0版本对多分辨率适配提供了很好的支持,使用起来 ...

  2. Win7无线网络共享设置方法

    http://jingyan.baidu.com/article/4f34706e89bb2ae387b56d0b.html

  3. 深入理解Binder(二),Binder是什么?

    上篇文章深入理解Binder(一),从AIDL谈起我们介绍了AIDL的基本使用,用AIDL两个App的通信是实现了,可是又有小伙伴疑惑了,为什么使用AIDL就能够实现两个App之间的通信?本文我们就来 ...

  4. Android_Gallery

    xml布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  5. jquery uploadify修改上传的文件名和显示

    如果觉得看文章太麻烦,可以直接看参考:http://stackoverflow.com/questions/7707687/jquery-uploadify-change-file-name-as-i ...

  6. 【转】android adb命令

      1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): andr ...

  7. jQuery mouseover与mouseenter,mouseout与mouseleave的区别

    mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...

  8. jsp实现计算器

    JavaBean package com.itheima.domain; import java.math.BigDecimal; public class BigDecimalDemo { priv ...

  9. 牵扯较多属性和方法的类题目,很简单的题目本来不想发的,如果有同学学到这个题目感觉太长不愿敲代码,copy走我的即可~不过还是建议自己打一打

    /* 3.设计一个"学生"类 1> 属性 * 姓名 * 生日 * 年龄 * 身高(单位是m) * 体重(单位是kg) * 性别 * C语言成绩 * OC成绩 * iOS成绩 ...

  10. asp.net 邮件发送类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...