Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 事件日志索引是否唯一_C#_Event Viewer - Fatal编程技术网

C# 事件日志索引是否唯一

C# 事件日志索引是否唯一,c#,event-viewer,C#,Event Viewer,我通过以下方式从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 co

我通过以下方式从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++;
  }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
事件日志事件日志;
eventLog=新的eventLog();
eventLog.Log=“安全性”;;
eventLog.Source=“安全审核”;
eventLog.MachineName=“服务器”;
var计数=0;
foreach(eventLog.Entries.Cast()中的EventLogEntry日志,其中(log=>log.InstanceId==4625))
{
Write(“eventLogEntry.Index:{0}{1}”,log.Index,Environment.NewLine);
保存记录(日志);
计数++;
}
}
我试图捕获服务器上的所有无效登录,然后在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;
}
//
///返回windows事件日志中与传入条件匹配的所有事件。
///如果未传入任何内容,则它将返回用户尝试的所有事件
///登录到计算机并提供了无效的用户名或密码。
/// 
///事件日志所在的计算机。
///列表的截止日期和时间。
///事件日志中的“日志名称”。这是事件所在的文件夹。
///事件日志“源”。
///筛选到事件日志中的特定“事件Id”。
/// 
公共静态IEnumerable GetEventLogs(字符串eventLogMachineName,
日期时间截止日期时间,
字符串eventLogName=“安全性”,
string eventLogSource=“安全审核”,
int instanceId=4625)
{
var eventLog=new eventLog{Log=eventLogName,Source=eventLogSource,MachineName=eventLogMachineName};
从eventLog.Entries中的EventLogEntry日志返回
其中log.InstanceId==InstanceId&&
log.TimeGenerated>cutoffdatetime
选择日志;
}
我这样称呼它:

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++;
  }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
var lastcheckdatetime=Properties.Settings.Default.LastCheckDate;
如果(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计数=0;
foreach(日志中的EventLogEntry l)
{
Console.WriteLine(“-------------------”;
Write(“eventLogEntry.Index:{0}{1}”,l.Index,Environment.NewLine);
Write(“eventLogEntry.TimeGenerated:{0}{1}”,l.TimeGenerated,Environment.NewLine);
计数++;
}
}

根据我的经验和很少的研究
Index
属性显示从创建事件日志开始编写的事件索引

但是有几件事你错过了

首先,您必须记住事件日志的大小是有限的。例如,假设“安全性”日志只能容纳1000个条目(如果您查看eventvwr.msc,则eventlog属性中显示的实际大小以mb为单位)。因此,当事件日志已满时,有3种方式:

  • 将新事件写在旧事件之上。在这种情况下,记住上次读取的事件索引是不好的。因为该索引指向的事件可以被简单地覆盖
  • 做一个档案。在这种情况下,记住的索引现在可以指向存档中的事件,而不是事件日志的当前.evtx文件中的事件
  • 不要写入新事件,手动清除事件日志。我认为这并不有趣,因为你想要一个自动化的工具
  • 因此,可以将eventlog设置为存档,并记住事件的最后一个索引。然后在再次读取事件日志时,首先获取当前事件日志文件的最旧记录:

    EventLog log = new System.Diagnostics.EventLog("Security");
    int oldestIndex = log.Entries[(int)eli.OldestRecordNumber].Index;
    
    然后将
    oldestinex
    与您的
    lastReadedIndex
    进行比较,如果
    lastReadedIndex
    您首先必须读取归档文件,而不是读取当前事件日志文件

    默认情况下,所有存档都存储在当前事件日志文件(.evtx)所在的同一目录中。通过使用,可以轻松读取档案。试着看一下,它是
    RecordId
    属性,我认为它与
    EventLogEntry
    类的
    Index
    属性相同(目前无法检查)

    另一种方法是记住事件写入的时间,并将其用作搜索新事件的起点,以防
    索引
    记录ID
    没有帮助


    祝你好运

    是的,我用了最后一次检查的日期。我将更新我的帖子,以便将来搜索我所做的事情。谢谢你的帮助!