我从Windows中的事件日志中获取信息:
private void button1_Click(object sender, EventArgs e)
{
EventLog eventLog;
eventLog = new EventLog();
eventLog.Log = "Security";;
eventLog.Source = "Security-Auditing";
eventLog.MachineName = "SERVER";
var count = 0;
foreach (EventLogEntry log in eventLog.Entries.Cast<EventLogEntry>().Where(log => log.InstanceId == 4625))
{
Console.Write("eventLogEntry.Index: {0}{1}", log.Index, Environment.NewLine);
SaveRecord(log);
count++;
}
}
我正在尝试捕获所有无效登录到我的服务器,然后在x次无效尝试次数后添加一个条目。
我正在遍历事件日志并获取信息而没有问题,但我怎么知道我停止阅读的最后一条记录是什么?当日志获取更多信息时,我需要重新开始阅读,但我需要一个起点。
我以为我可以在EventLogEntry上使用索引,但我找不到任何关于它的信息。我在机器上的那些数字为6位数字。
那有多可靠?我应该去别的吗?我读完之后应该清除那个日志吗?
感谢您的投入!
=======我发了什么========
在Apokal的帮助下,我就是这样做的:
/// <summary>
/// Returns all events in the windows event log that match the passed in criteria.
/// If nothing is passed in then it will return all events where the a user attemtped
/// to log into the the machine and gave an invalid username or password.
/// </summary>
/// <param name="eventLogMachineName">The machine where the event log is at.</param>
/// <param name="cutoffdatetime">Date and time of the cut off for the list.</param>
/// <param name="eventLogName">'Log Name' in the event log. This is the folder that the events reside in.</param>
/// <param name="eventLogSource">Event log 'Source'.</param>
/// <param name="instanceId">Filters to a specific 'Event Id' in the event log.</param>
/// <returns></returns>
public static IEnumerable<EventLogEntry> GetEventLogs(string eventLogMachineName,
DateTime cutoffdatetime,
string eventLogName = "Security",
string eventLogSource = "Security-Auditing",
int instanceId = 4625)
{
var eventLog = new EventLog {Log = eventLogName, Source = eventLogSource, MachineName = eventLogMachineName};
return from EventLogEntry log in eventLog.Entries
where log.InstanceId == instanceId &&
log.TimeGenerated > cutoffdatetime
select log;
}
我称之为:
private void button1_Click(object sender, EventArgs e)
{
var lastcheckdatetime = Properties.Settings.Default.LastCheckDate;
if (lastcheckdatetime < (DateTime.Now.AddDays(-30)))
{
lastcheckdatetime = DateTime.Now.AddDays(-7);
}
var log = EventLogClass.GetEventLogs("TGSERVER", lastcheckdatetime);
Properties.Settings.Default.LastCheckDate = DateTime.Now;
Properties.Settings.Default.Save();
var count = 0;
foreach (EventLogEntry l in log)
{
Console.WriteLine("---------------------");
Console.Write("eventLogEntry.Index: {0}{1}", l.Index, Environment.NewLine);
Console.Write("eventLogEntry.TimeGenerated: {0}{1}", l.TimeGenerated, Environment.NewLine);
count++;
}
}
答案 0 :(得分:1)
从我的经验和小研究Index
属性显示从事件日志创建开始写入的事件索引。
但是你错过了几件事。
首先,您必须记住事件日志的大小有限。例如,假设“安全”日志只能容纳1000个条目(如果查看eventvwr.msc,则事件日志属性中显示的实际大小以mb为单位)。所以,当事件日志已满时,有3种方法:
因此,可以将事件日志设置为存档并记住事件的最后一个索引。然后,当再次读取事件日志时,首先获取当前事件日志文件的最旧记录:
EventLog log = new System.Diagnostics.EventLog("Security");
int oldestIndex = log.Entries[(int)eli.OldestRecordNumber].Index;
然后将oldestIndex
与您的lastReadedIndex
进行比较,如果lastReadedIndex < oldestIndex
首先需要阅读档案,而不是阅读当前的事件日志文件。
默认情况下,所有存档都存储在当前事件日志文件所在的同一目录中(.evtx)。使用EventLogReader class可以轻松呈现档案。尝试查看EventRecord及其RecordId
属性,我认为它与Index
类的EventLogEntry
属性相同(目前无法检查)。
另一种方法是记住编写事件的时间,并将其用作搜索新事件的起点,以防Index
和RecordId
无效。