Thursday, May 25, 2006

CSharp log parsing file

//Jonathan Mark
//NG Midtier Team
//May 2006
//Application for reading customized log files.

//The application looks for phrases which indicate that a processor is entering a period
// of inactivity. The next timed log entry after such a shutdown is a startup.

// Line 3114 of allergy_060514 is out of order. A later time appears before an earlier one.

//This degenerate log condition interferes with the logic of the application, which
// assumes that each row occurred at or after the time of the row above it.

// Sorting files with potentially hundreds of thousands of rows in a client .NET application
// could be impractical. Therefore, this application first checks to make certain that the log
// is sorted. If not it stops processing and alerts the user to sort the file.

// File allergy_060513 has corrupted dates which begin with the numerals 00. These need to be
//removed prior to sorting. If not the application stops processing and alerts the user to
// sort the file.

using System;
using System.Collections;
using System.IO;
using System.Reflection;
using MbUnit.Core;
using MbUnit.Framework;
using System.Text.RegularExpressions;



[TestFixture]
class ParseLog
{


public TimeSpan totalOnTime = (DateTime.Now - DateTime.Now);
public TimeSpan totalOffTime = (DateTime.Now - DateTime.Now);
string myLine = "";
int iMonth;
int iDay;
int iYear;
int iHour;
int iMinute;
int iSec;
public DateTime lastStart;
public DateTime lastFinish;
int numberOfStarts = 0;

public ParseLog()
{
Console.WriteLine("Starting Processing...");
}


[Test] public DateTime ConvertToDate(string snippet)
// i is the row of the file being processed. Currently only used for debugging.
{
//use of offsets deprecated
if (snippet.Substring(3,1) == "/") // Some dates are corrupted and
// have an extra zero in front.
{
//This is a corrupt row, as its month numeral has three digits
Assert.AreEqual(1,0, "The input file has a corrupt date that starts with an extra zero.");
}
DateTime dtInSnippet;
iMonth = Int32.Parse(snippet.Substring(0, 2));
iDay = Int32.Parse(snippet.Substring(3, 2));
iYear = Int32.Parse(snippet.Substring(6, 4 ));
iHour = Int32.Parse(snippet.Substring(11, 2 ));
iMinute= Int32.Parse(snippet.Substring(14, 2 ));
iSec = Int32.Parse(snippet.Substring(17, 2 ));
dtInSnippet=new DateTime(iYear,iMonth,iDay,iHour,iMinute,iSec);
return dtInSnippet;
}



public bool IsFinished(string strFinished)
//Tests for phrases indicating Processor is shutting down
{
Regex objNotWholePattern1=new Regex("Finished processing");
if(objNotWholePattern1.IsMatch(strFinished))
{
lastFinish = ConvertToDate(strFinished);
TimeSpan recentInterval = lastFinish - lastStart;
totalOnTime = totalOnTime + recentInterval;
numberOfStarts = 0;
}
else
{
Regex objNotWholePattern2=new Regex("Starting processing");
if ((IsADate(strFinished)) & (numberOfStarts == 0) & objNotWholePattern2.IsMatch(strFinished))
{
lastStart = ConvertToDate(strFinished);
numberOfStarts = 1;
}

}
return objNotWholePattern1.IsMatch(strFinished);
}








public bool IsADate(string strIsADate)
{
Regex objNotWholePattern3=new Regex("^[0-9][0-9]");

return objNotWholePattern3.IsMatch(strIsADate);
}




[Test] void readLog(String filename)
{


Console.WriteLine("Reading File...");
StreamReader srRead = File.OpenText(filename);
srRead.BaseStream.Seek(0, SeekOrigin.Begin);

while (srRead.Peek() > -1)
{
myLine = srRead.ReadLine() + "\n";
myLine = myLine.Trim();
if (IsFinished(myLine))
{
Console.WriteLine(myLine + totalOnTime.ToString());
}

}
srRead.Close();
Console.WriteLine("Finished reading File...");
}




///
/// Summary description for Class1.
///

class Class1
{
///
/// The main entry point for the application.
///

[STAThread]

static void Main(string[] args)
{
ParseLog myParseLog = new ParseLog();
myParseLog.readLog("c:\\logs\\allergy_060513.log");
Console.WriteLine(myParseLog.totalOnTime.ToString());
int x = 1;
while (x == 1)
{
x = 1;
}

}
}

}

0 Comments:

Post a Comment

<< Home