.NET 操作 EventLog(Windows事件日志监控)(转载)
操作Windows日志:EventLog
如果要在.NET Core控制台项目中使用EventLog(Windows事件日志监控),首先需要下载Nuget包:
此外执行程序要拥有管理员权限
1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”、“Internet Explorer”、“安全性”和“系统”都是日志(严格地说是日志的显示名字)
2:事件源:列表中的“来源”,创建时和事件日志相关联;
3:事件类型:包括“信息”、“错误”等;
基本操作:
1:创建日志:我没找到直接创建日志的方法,日志应该都是通过下面的创建事件源来间接创建;
2:创建事件源:静态方法EventLog.CreateEventSource(string sourceName, string LogName); //参数分别表示事件源名和日志名
功能说明:在某个事件日志中创建事件源,如果事件日志不存在,则自动创建;
3:删除日志:静态方法EventLog.Delete(string logName);
4:删除事件源:静态方法EventLog.DeleteEventSource(string sourceName);
5:判断日志是否存在:静态方法EventLog.Exists(string logName);
6:判断事件源是否存在:静态方法EventLog. SourceExists (string sourceName);
7:写日志:使用EventLog类的实例调用方法WriteEntry(string logDesc, EventLogEntryType.Information); //或者EventLogEntryType.Error
基本用法:
using System;
using System.Diagnostics; namespace Event
{
class Program
{
static void WriteError()
{
//检测Windows日志中是否存在事件源:My Application,如果不存在就创建事件源
if (!EventLog.SourceExists("My Application"))
{
EventLog.CreateEventSource("My Application", "Application");//创建事件源,事件源名:My Application,事件日志名:Application
} var log = new EventLog("Application")//创建日志到事件日志:Application
{
Source = "My Application"//指定日志的事件源:My Application
}; log.WriteEntry("Error", EventLogEntryType.Error);
} static void Main(string[] args)
{
WriteError(); Console.WriteLine("Press any key to quit!");
Console.ReadKey();
}
}
}
测试:
using System;
using System.Diagnostics; namespace WindowsConsoleApp
{
//测试
public class EnventLogHelper
{
private EventLog log; public EnventLogHelper()
{
log = new EventLog();//默认写应用程序日志
}
public EnventLogHelper(string name)
{
log = new EventLog(name);//指定写入的分类,用户自定义则新建分组。系统保留//"Application"应用程序, "Security"安全, "System"系统
//或者可以用 log.Log = "Security";指定
} public void WriteToApp()
{
try
{ log.Source = "我的应用程序";//日志来源
log.WriteEntry("处理信息1", EventLogEntryType.Information);//日志类型
log.WriteEntry("处理信息2", EventLogEntryType.Information);
throw new System.IO.FileNotFoundException("readme.txt文件未找到");
}
catch (System.IO.FileNotFoundException exception)
{
log.WriteEntry(exception.Message, EventLogEntryType.Error); }
} public void ReadLog()
{
EventLogEntryCollection eventLogEntryCollection = log.Entries;//获取日志collection
foreach (EventLogEntry entry in eventLogEntryCollection)
{ string info = string.Empty; info += "【类型】:" + entry.EntryType.ToString() + ";";
info += "【日期】" + entry.TimeGenerated.ToLongDateString() + ";";
info += "【时间】" + entry.TimeGenerated.ToLongTimeString() + ";"; info += "【计算机】" + entry.MachineName + "【来源】" + entry.Source + "【详细信息】" + entry.Message + "【】";
//
Console.WriteLine(info); }
} }
}
监控Windows日志增量变化:EventLogWatcher
using System;
using System.Diagnostics.Eventing.Reader; namespace WindowsConsoleApp
{
class SubscribeToEventsExample
{
static void Main1(string[] args)
{
//监控类
EventLogWatcher watcher = null; try
{
// Xpath语法筛选目标事件的发生
EventLogQuery subscriptionQuery = new EventLogQuery(
"Application", PathType.LogName, "*[System/Level=2] or *[System/Level=3]"); watcher = new EventLogWatcher(subscriptionQuery); // 订阅到事件发生时候,触发事件
watcher.EventRecordWritten +=
new EventHandler<EventRecordWrittenEventArgs>(
EventLogEventRead); //开始订阅Windows日志
watcher.Enabled = true; //如果不停止,监控类会不停查询时间发生,直到Enable设置为false
for (int i = ; i < ; i++)
{
// Wait for events to occur.
System.Threading.Thread.Sleep();
} }
catch (EventLogReadingException e)
{
Console.WriteLine("Error reading the log: {0}", e.Message);
}
finally
{
// 停止监控
watcher.Enabled = false; if (watcher != null)
{
watcher.Dispose();
}
}
} /// <summary>
/// 事件触发
/// </summary>
public static void EventLogEventRead(object obj, EventRecordWrittenEventArgs arg)
{
// Make sure there was no error reading the event.
if (arg.EventRecord != null)
{
Console.WriteLine("Received event {0} from the subscription.",
arg.EventRecord.Id);
Console.WriteLine("Description: {0}", arg.EventRecord.FormatDescription()); //log.EventId = arg.EventRecord.Id;//系统日志分配的记录ID
//log.Source = arg.EventRecord.ProviderName;//来源
//log.Level = (int)(arg.EventRecord.LevelDisplayName == "错误" ? WinLogLevelID.ERROR : WinLogLevelID.WARN);
//log.TaskName = arg.EventRecord.TaskDisplayName ?? "无";
//log.LogMessage = arg.EventRecord.FormatDescription();
//log.TimeCreate = arg.EventRecord.TimeCreated ?? DateTime.Now;
}
else
{
Console.WriteLine("The event instance was null.");
}
}
}
}
查询规则: https://msdn.microsoft.com/en-us/library/bb399427.aspx
源码:https://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/EventLog.cs
EventLog:https://msdn.microsoft.com/zh-cn/library/system.diagnostics.eventlog(v=vs.110).aspx
EventQuery:
https://msdn.microsoft.com/en-us/library/bb671200.aspx
EventLogReader:
https://msdn.microsoft.com/zh-cn/library/system.diagnostics.eventing.reader.eventlogreader(v=vs.110).aspx
.NET 操作 EventLog(Windows事件日志监控)(转载)的更多相关文章
- .NET拾忆:EventLog(Windows事件日志监控)
操作Windows日志:EventLog 1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”.“Internet Explorer”.“安全性”和“系统”都是日志(严格地说是日 ...
- syslog系统日志、Windows事件日志监控
- 控制台——EventLog实现事件日志操作
我们应该如何通过写代码的方式向其中添加“日志”呢? 在操作之前,先明确几个概念: 1:事件日志名(logName):“事件查看器”中的每一项,如“应用程序”.“Internet Explorer”.“ ...
- 使用EventLog类写Windows事件日志
在程序中经常需要将指定的信息(包括异常信息和正常处理信息)写到日志中.在C#3.0中可以使用EventLog类将各种信息直接写入Windows日志.EventLog类在System.Diagnosti ...
- 【第一章】zabbix3.4监控WindowsCPU使用率磁盘IO磁盘事件日志监控阈值邮件报警详细配置
Windows安装zabbix-agent 监控Windows-CPU使用率 监控Windows-磁盘IO性能监控 监控Windows/Linux-磁盘触发器阈值更改 监控Windows-网卡自动发现 ...
- Syslog和Windows事件日志收集
Syslog和Windows事件日志收集 EventLog Analyzer从分布式Windows设备收集事件日志,或从分布式Linux和UNIX设备.交换机和路由器(Cisco)收集syslog.事 ...
- 为什么要使用日志管理?syslog和Windows事件日志
为什么要使用日志管理?syslog和Windows事件日志 日志管理 - 确保网络安全的先决条件 日志给予您有关网络活动的第一手信息.日志管理确保日志中隐藏的网络活动数据转换为有意义的可操作的安全信息 ...
- Python处理Windows事件日志(json)
通过NXlog将Windows事件日志保存为json格式文件,然后在Python中使用json.loads()进行处理. NXlog在将Windows事件日志保存为json格式文件,文件中带入了BOM ...
- EventLog实现事件日志操作
选中”我的电脑”,在其右键菜单中选择“管理”,在打开的对话框中包括了如下图所示的“日志”信息: 选中其中的某一条日志,可以看到如下的详细信息: 我们应该如何通过写代码的方式向其中添加“日志”呢? 在操 ...
随机推荐
- html打造动画【系列3】- 小猫笑脸动画
猫咪容器 咱们每次画一个图片,肯定先要确定一个容器,几确定一下图形的位置和大小. <div class="mao_box"> <div class="m ...
- 纯css修改复选框默认样式
input[type='checkbox']{ width: 20px; height: 20px; background-color: #fff; -webkit-appearance:none; ...
- TileStache生成切片
1.tilestache.cfg { "cache": { "name": "Disk", "path": " ...
- 数字时钟(DigitalClock)
数字时钟(DigitalClock) 这个其实就是我们平时看到的手机上面显示的时间 很简单 1.Activity //数字时钟 public class DigitalClockActivity ex ...
- Linux kernel pwn notes(内核漏洞利用学习)
前言 对这段时间学习的 linux 内核中的一些简单的利用技术做一个记录,如有差错,请见谅. 相关的文件 https://gitee.com/hac425/kernel_ctf 相关引用已在文中进行了 ...
- 从源码上分析ListView的addHeaderView和setAdapter的调用顺序
ListView想要添加headerview的话,就要通过addHeaderView这个方法,然后想要为ListView设置数据的话,就要调用setAdapter方法了.但是,在调用addHeader ...
- pycharm 调试Django 奇葩问题:Process finished with exit code -1073741819
想自己整个BLOG,发现python+Django好像还不错,尝试一下.在使用过程中,突然pycharm不能调试django工程.网上搜索也没解决,是google哦.好像记得启动pycharm时,看到 ...
- SQLSERVER性能计数器的简单剖析
SQLSERVER性能计数器的简单剖析 今晚看了这篇文章:SQL Server 2012新performance counter:非常实用的Batch Resp Statistics 文章里介绍到SQ ...
- 第八章 SQL高级处理 8-2 GROUPING运算符
一.同时得到合计行 合计行是不指定聚合键时得到的汇总结果. UNION ALL与UNION的不同之处是它不会对结果进行排序,因此比UNION性能更好. 二.ROLLUP——同时得出合计和小计 GR ...
- Jmeter入门--断言(检查点)
断言是在请求的返回层面增加一层判断机制.因为请求成功,并不代表结果一定正确,因为此需要检查机制提高测试准确性. 1.响应断言 模式匹配规则: 包括:返回结果包括你指定的内容,支持正则匹配 例如: 响应 ...