I'm trying to go through my event logs with a Parallel.ForEach. But at random it stops with the error IndexOutOfRangeException on the foreach.
this one:
foreach (EventLogEntry message in evlog.Entries)
Sometimes it stops with the first time it goes through the loop and sometimes after already gone through the loop 44 times.
Also I sometimes get the error that the given ID already exists.
Here's my full code:
string ID = null;
Logs.Add("System", 0);
Logs.Add("Application", 1000);
Logs.Add("Setup", 2000);
Logs.Add("Forwarded Events", 3000);
EventLog evlog = new EventLog();
evlog.MachineName = ".";
Parallel.ForEach(Logs.Keys, logname =>
{
System.Threading.Thread.Sleep(1000);
evlog.Source = logname;
string lognameWithoutSpaces = logname.Replace(" ", "");
foreach (EventLogEntry message in evlog.Entries)
{
string type = message.EntryType.ToString();
if (type == "0" | type == "Warning")
{
ID = lognameWithoutSpaces + "_" + Logs[logname].ToString();
Console.WriteLine("ID: " + ID);
dictLogs.Add(ID, new List<string>());
dictLogs[ID].Add(evlog.Log);
dictLogs[ID].Add(message.Source);
dictLogs[ID].Add(message.InstanceId.ToString());
dictLogs[ID].Add(type);
dictLogs[ID].Add(message.UserName);
dictLogs[ID].Add(message.TimeGenerated.ToString());
dictLogs[ID].Add(message.Category);
dictLogs[ID].Add(message.MachineName);
dictLogs[ID].Add(message.Message);
Logs[logname]++;
}
}
});
dictLogs.Clear();
This is what I see in de console:
ID: ForwardedEvents_3000
ID: System_0
ID: Setup_2000
ID: Application_1000
'DashboardBackEnd.vshost.exe' (CLR v4.0.30319: DashboardBackEnd.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.resources\v4.0_4.0.0.0_nl_b77a5c561934e089\System.resources.dll'. Module was built without symbols.
A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IndexOutOfRangeException' occurred in System.dll