事件日志从用户计数器

时间:2016-10-12 08:22:28

标签: c# .net

早上好。我仍然反对这个拦截器,所以我试图再次提问,但这一次更清楚。

public string User { get; set; }
public int countUsers { get; set; }
public Users(int count, string name)
{
    countUsers = count;
    User = name;
}

public void getCountUsers()
{
    number = 0; //
    UserList = new ObservableCollection<Users>();
    EventLog myNewLog = new EventLog(); 
    myNewLog.Log = "Security";
    foreach (EventLogEntry entry in myNewLog.Entries)
    {
        if (entry.InstanceId == 4624 && entry.TimeWritten.Date == DateTime.Today)
        {
            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.Add(u);
            }
        }
    }
}

public void amount()
{
    var totalUsers = UserList.Sum(user => user.countUsers);
    Console.WriteLine("There has been {0} users on {1}",
    totalUsers, DateTime.Today.ToShortDateString());
}

public void amountPer()
{
    foreach (var user in UserList.Where(u => u.User != "SYSTEM"))
    {
        Console.WriteLine("User  {0} has been online {1}x 
     times",user.User,user.countUsers);
    }

}

我现在拥有的是什么。我面临的是,我需要弄清楚如何过滤掉金额,就像我在amountPer中所做的那样。

然而,我不想在每个循环中使用a,因为我需要一个通用数字。 What it outputs currently 这包括名称System,我想要过滤掉它,因此它只计算实际用户。我该怎么做?

1 个答案:

答案 0 :(得分:0)

要仅过滤NOT系统用户,您只需将Count与lambda表达式一起使用,以排除SYSTEM用户

public void amount()
{

    var totalUsers = UserList.Count(user => user.User != "SYSTEM");
    Console.WriteLine("There has been {0} users on {1}",
    totalUsers, DateTime.Today.ToShortDateString());
}

然而,这并不意味着您的代码不会循环播放。循环仍由Count扩展

隐藏