您好,我一直在摆弄事件日志方法,并且在摆弄它的时候,我能够计算出有多少个entry.replacementstrings5用户名会在那里。
public int countUsers { get; set; }
public string User { 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 counter()
{
var totalUsers = UserList.Sum(user => user.countUsers);
foreach (var user in UserList)
{
Console.WriteLine("There has been {0} users on {1}", user.countUsers, DateTime.Today.ToShortDateString());
}
}就是我目前所拥有的。我现在想要做的是,向writeline添加一个正则表达式,这样它就不会计算用户系统。
我可以这样做,但这将打印出每个单独的用户,但我想要的是在指定日期有多少人在线的总体/全局想法。
所以我需要知道如何去掉for each循环,只获取user.countUsers。
foreach (var user in UserList)
{
Regex User = new Regex(@"SYSTEM");
Match match = User.Match(user.User);
if (!match.Success)
{}
}我现在不知道如何调用这个变量,所以我的正则表达式可以工作。有谁知道如何修复它,也许不需要正则表达式?
(旁注:我也需要帮助,因为EventLog是2x,而合法的应该是1。我需要看看我将如何过滤它)
发布于 2016-10-06 18:12:58
你不需要使用正则表达式,因为你想排除的字符串是文字,你可以使用Linq:
foreach (var user in UserList.Where(u => u.User != "SYSTEM"))
{
Console.WriteLine("There has been {0} users on {1}", user.countUsers, DateTime.Today.ToShortDateString());
}https://stackoverflow.com/questions/39892935
复制相似问题