EventLog组件
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组件的更多相关文章
- 使用EventLog组件向本机现有日志中添加条目
实现效果: 知识运用: EventLog组件的MachineName属性 //获取或设置在其上读取或写入事件的计算机名称 public string MachineName {get;set; } ...
- EventLog组件读写事件日志
使用.Net中的EventLog控件使您可以访问或自定义Windows 事件日志,事件日志记录关于重要的软件或硬件事件的信息.通过 EventLog,可以读取现有日志,向日志中写入项,创建或删除事件源 ...
- 使用EventLog组件保存Windows系统日志
实现效果: 知识运用: EventLog类的CreateEventSource方法 //用于建立一个应用程序 使用指定的Sourc作为向本机上的日志中写入日志项的有效事件源 CreateEventS ...
- 使用EventLog组件读写事件日志
实现效果: 知识运用: Eventlog类的SourceExists方法 //确定指定的事件源是否已在本地计算机注册 public static bool SourceExists(string s ...
- 一文了解如何源码编译Rainbond基础组件
Rainbond 主要由以下三个项目组成,参考官网详细 技术架构 业务端 Rainbond-UI 和 Rainbond-Console 合起来构成了业务层.业务层是前后端分离模式.UI是业务层的前端代 ...
- 如何用.NET创建Windows服务
我们将研究如何创建一个作为Windows服务的应用程序.内容包含什么是Windows服务,如何创建.安装和调试它们.会用到System.ServiceProcess.ServiceBase命名空间的类 ...
- 服务器重启后SQL Server Agent由于"The EventLog service has not been started" 启动失败
案例环境: 操作系统 : Microsoft Windows Server 2003 Standard Edtion SP2 数据库版本 : SQL Server 2005 Standard Ed ...
- web 前端常用组件【01】Pagination 分页
分页组件几乎是一般网站都会涉及到的组件,网上有很多这样的插件,自己挑来跳去选择了这一款. 官方Demo网址:http://mricle.com/JqueryPagination 功能强大,可扩展性比较 ...
- 添加无线服务wzcsvc服务,Eventlog服务
<添加eventlog服务.reg> Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentCont ...
随机推荐
- C#_delegate - 有返回值手工调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Using zend-paginator in your Album Module
Using zend-paginator in your Album Module TODO Update to: follow the changes in the user-guide use S ...
- next nextval
1 KMP算法中next与nextval值的计算 以上两张图代表了next值的求法,本人总结后做如下叙述: 根据公式可知: next[1]=0 next[2]=1 next[3]的求法根据公式可以直接 ...
- android开发之路03
一.Activity1.如何在一个应用程序中定义多个Activity:①定义一个类,继承Activity:②在该类当中,复写Activity当中的onCreate方法:③在AndroidManifes ...
- CentOS7 发布 ASP.NET MVC 4 --- mono 4.6.0 + jexus 5.8.1
yum -y install gcc gcc-c++ yum -y install bison pkgconfig glib2-devel gettext make libpng-devel libj ...
- WPF 中的绑定方式
1.元素间的绑定 xaml方式 <Slider Name="slider1" Value="20"/> <TextBlock T ...
- android之模拟器更新底层
Android源码修改,编译后得到system.img 替换\AndroidSDK\system-images\android-21\android-tv\armeabi-v7a\目录中的system ...
- .Net 中表达式的转换
.Net 中表达式的转换 如: a>0 && (c>a || a <b ) || (a>b || c>1) 转换后 (((a > 0) a ...
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- Oracle12c创建新用户提示公共用户名或角色无效
今天将备份的数据库还原到一台新的电脑上,首先要创建用户,执行如下语句: create user fxhy identified " default tablespace USERS temp ...