Tuesday, October 16, 2012

Read Event Log Information using LINQ PAD


Anyone tried the LINQ PAD. I found this is very useful to execute for query any short of information from different sources like database, xml, event logs, iis logs with the minimal code

In this blog, i am going to show a example, how we could pull the summary of the events using the LINQ PAD

Link to download LINQ PAD http://www.linqpad.net/


LINQPad is a free software utility for Microsoft .NET to interactively query SQL databases and other data sources such as OData or WCF Data Services using LINQ. LINQPad supports the following LINQ dialects:
  • LINQ to Objects
  • LINQ to SQL
  • Entity Framework
  • LINQ to XML
It also supports writing queries in SQL as well.

Download and install the LINQ PAD. Open the LINQ PAD and select a new query and select the language as C# Program

Code



void Main()
{
var serverList = new List
  {
  //List of Servers Name
  };
var eventCollection = new Dictionary();

int count = 0;
foreach (var list in serverList)
{
var eventLog = new EventLog(EVENTLOGNAME, list, SOURCENAME);
        foreach (EventLogEntry logEntry in eventLog.Entries)
        {
if (logEntry.TimeGenerated  >=  DateTime.Today)
{
if (eventCollection.ContainsKey(logEntry.InstanceId))
{
eventCollection[logEntry.InstanceId]++;
}
else
{
eventCollection.Add(logEntry.InstanceId, 1);
}
}

        }
Console.WriteLine("Summary of Server " + list);
foreach (var logs in eventCollection)
{
Console.WriteLine(logs.Key + "  " + logs.Value);
}
eventCollection.Clear();
}

}

// Define other methods and classes here


Sample Output

Summary of the Server "ServerName1"
Event ID   Number Of Times
100            7
106            15

Summary of the Server "ServerName2"
Event ID   Number Of Times
100            70
106            8

No comments:

Post a Comment