我通过以下方式从 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++;
}
}