Some time ago, our web servers were throwing ALOT of errors that were getting logged in some of our log files. This became a somewhat high priority issue because it was impacting the performance of our sites. A team of us were asked to dig into the problem and try to figure out where the problem was. Problem was, the log files were massive. I ended up having to write a small program to parse out some of the more important information for everyone (seen below). This was a fun exercise in reading and writing from/to a file and parsing strings.
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace FileParser
{
class Program
{
static void Main(string[] args)
{
//Name and path of the file to write out our results.
String fileToWrite = "D:\\downloads\\output.txt";
//Name and path of the log file.
String fileToOpen = "D:\\downloads\\DistributedTroubleshooting.log";
String textToSearchFor = "dbo.";
//List to hold what we find in the file.
List<contentsOfFile> contents = new List<contentsOfFile>();
List<String> s = new List<String>();
List<String> time = new List<String>();
TextWriter tw = new StreamWriter(fileToWrite);
string line = "";
using (StreamReader sr = new StreamReader(fileToOpen))
{
//As long as there is another entry in the file, keep going.
while ((line = sr.ReadLine()) != null)
{
//Split the line returned on the quotation mark.
String[] sentence = line.Split(‘"’);
//If we find what we are looking for, split the second portion of the sentence again.
if (line.Contains(textToSearchFor))
{
String[] sentence2 = sentence[2].Split(‘a’, ‘t’, ‘ ‘);
//Add what we found for later use.
contents.Add(new contentsOfFile(sentence[1], sentence2[4]));
}
if (line.Contains("unloaded"))
{
parseContents(contents, tw);
contents.Clear();
tw.WriteLine("");tw.WriteLine("Unloaded");tw.WriteLine("");
}
}
}
}
static void parseContents(List<contentsOfFile> s, TextWriter tw)
{
int nameLength=0;
int length = s.Count();
int occurenceCount=0;
int totalOccurences = 0;
String alreadyVisited="";
String compareContents="";
//Loop through and get the number of occurences of our error.
for (int x = 0; x < length; x++)
{
compareContents=s[x].name;
String time = s[x].time;
if (!alreadyVisited.Contains(compareContents)){
alreadyVisited = alreadyVisited + s[x].name;
for (int y = 0; y < length; y++)
{
if (s[y].name == compareContents)
{
occurenceCount += 1;
}
}
nameLength = compareContents.Length;
for (int z = 0; z < 50 – nameLength; z++)
{
compareContents += " ";
}
//Output the time of the occurence.
tw.WriteLine(compareContents + "(" + time + "): " + occurenceCount);
occurenceCount = 0;
}
totalOccurences += 1;
}
//Output the total occurences.
tw.WriteLine("");tw.WriteLine("Total Occurences: " + totalOccurences);
totalOccurences = 0;
}
}
}
public class contentsOfFile
{
public string name;
public string time;
public contentsOfFile(string s, string time)
{
this.name = s;
this.time = time;
}
}
Related posts: