计算日期时间之间出现的次数

Counting how many occurences have been between datetimes

 public void getCountUsers()
    {
        number = 0;
        //UserList = new ObservableCollection<Users>();
        UserList = new List<Users>();
        EventLog myNewLog = new EventLog();
        myNewLog.Log = "Security";
        foreach (EventLogEntry entry in myNewLog.Entries)
        {
            if (entry.InstanceId == 4624 && entry.TimeWritten.Date == DateTime.Today)
            {
                Regex ip = new Regex(@"192");
                Match IP = ip.Match(entry.ReplacementStrings[18]);
                Regex tester = new Regex(@"127");
                Match Tester = tester.Match(entry.ReplacementStrings[18]);
                if (Tester.Success || IP.Success)
                {
                    if (UserList.Count > 0)
                    {
                        bool check = false;
                        foreach (var user in UserList)
                        {
                            if (user.User == entry.ReplacementStrings[5])
                            {
                                user.countUsers += 1;
                                check = true;
                            }
                        }
                        if (!check)
                        {
                            Users u = new Users(1, entry.ReplacementStrings[5]);
                            UserList.Add(u);
                        }
                    }
                    else
                    {
                        Users u = new Users(1, entry.ReplacementStrings[5]);
                        //UserList = new ObservableCollection<Users>();
                        UserList = new List<Users>();
                        UserList.Add(u);
                    }
                }
            }
        }
    }

这是我用来统计一天有多少用户的。我需要检索的是如何过滤当天的高峰时段,以及那个高峰时段有多少人在线,其中有哪些人。

(比如12:00有1个用户,15:00有15个,15:00是高峰期,一共15个用户)

如果您需要更多信息,我很乐意编辑。

编辑:

  foreach (EventLogEntry entry in myNewLog.Entries)
        {

            if (entry.InstanceId == 4624 && entry.TimeWritten.Date == DateTime.Today)
            {
                Regex ip = new Regex(@"192");
                Match IP = ip.Match(entry.ReplacementStrings[18]);
                string userName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(entry.ReplacementStrings[5]);

                Regex tester = new Regex(@"127");
                Match Tester = tester.Match(entry.ReplacementStrings[18]);

                if (IP.Success || Tester.Success)
                {
                    Console.WriteLine(number +
                     ". \t[Time of logon]\t\t" + entry.TimeWritten.ToLongTimeString() + "\t" + entry.TimeWritten.ToShortDateString() +
                     "\n   \t[Username-logon]\t" + userName +
                     "\n \t[IP-Adress]\t\t" + entry.ReplacementStrings[18] +
                     "\n----------------------------------------------------------------------- ");
                    number++;
                    //Console.WriteLine(entry.TimeWritten.ToLongTimeString() + " ; " + entry.TimeWritten.ToShortDateString() + " ; " + entry.ReplacementStrings[5] + " ; " + entry.ReplacementStrings[18]);
                }
            }
        }

上面输出第一部分,下面输出第二部分。

public void showAmount()
    {
        int indexUsername = 0;
        ++indexUsername;
        Console.WriteLine("\nGebruikers:");
        foreach (var user in UserList.Where(u => u.User != "SYSTEM"))
        {
            string userName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(user.User);
            //Console.WriteLine(indexUsername + ". " +" \t " + userName +"" + user.countUsers);
            Console.WriteLine("#" +indexUsername + ".\t   {0} has been online {1}x times ", userName, user.countUsers);
            indexUsername++;
        }
    }

Output

这就是你想要的吗?

public void getCountUsers()
{
    EventLog myNewLog = new EventLog();
    myNewLog.Log = "Security";

    var query =
        from EventLogEntry entry in myNewLog.Entries
        where entry.InstanceId == 4624
        where entry.TimeWritten.Date == DateTime.Today
        where new[] { "192", "127" }.Any(x => entry.ReplacementStrings[18].Contains(x))
        select new { entry.TimeWritten.Hour, User = entry.ReplacementStrings[5] };

    var result =
        query
            .GroupBy(x => x.Hour, x => x.User)
            .OrderByDescending(x => x.Count())
            .Take(1)
            .Select(x => new
            {
                Hour = x.Key,
                Users =
                    x
                        .GroupBy(y => y)
                        .Select(y => new Users(y.Count(), y.Key))
                        .ToList()
            })
            .First();

    Hour = result.Hour;
    UserList = result.Users;

}

query 有效地替换了您的代码。它在 myNewLog.Entries 上执行循环并提取用于创建 Users 对象的 entry.ReplacementStrings[5] 值。我已将 entry.TimeWritten.Hour 值添加到此数据中。

现在在 result 变量中,我采用 query 并将 User 属性 按 Hour 分组。然后我按每个小时时段的用户数降序排序,当我 .Take(1) 时,我只得到用户最多的小时。然后我 select 小时并做另一组用户来创建 Users 对象 - 就像你在代码中所做的那样 - 最后我用 .First() 来获得人口最多的小时。

最后两个作业只是为了模仿您的代码。


这是一种生成输出字符串的方法:

string output =
    String.Format(
        "Most active users were online on {0},{1} The users online at the peak was: {2}",
        TimeSpan.FromHours(result.Hour),
        result.Users.Count(),
        String.Join(", ", result.Users.Select(x => x.User)));