计算日期时间之间发生的次数

时间:2016-10-24 09:58:41

标签: c# datetime event-log

 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

1 个答案:

答案 0 :(得分:0)

这是你之后的事情吗?

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上的循环,并拉出用于创建entry.ReplacementStrings[5]对象的Users值。我已将entry.TimeWritten.Hour值添加到此数据中。

现在在result变量中,我使用query并将User属性分组为Hour。然后我按每小时插槽中用户数量的下降顺序排序,当我.Take(1)时,我只获得最多用户的小时数。然后我选择小时并进行另一组用户创建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)));