Logfile Parser Application seeking advice

Hi All

I have been tasked with a Xojo project and looking for some advice on best method to accomplish the following.

I need to parse my IIS webserver logfile (high transactional site) every 5 minutes to loop through search for and make a total count of a string “BJLive/Default.aspx?”
bearing in mind that the log file is anything up to 500MB in size.

Thanks

Lee

Textinputstream.ReadLine (in combination with instr()) might be a good way to go. I believe it doesn’t load the whole file into memory, just one line at a time. Due to the lack of an example file of that size I can’t test speed.

Thanks Alex do you know if there are any code snippets floating around somewhere to reference?

Thanks

Lee

Have a look at the wiki page, there is one… http://documentation.xojo.com/index.php/TextInputStream.ReadLine

This might be a start (untested):

Dim f As FolderItem Dim textInput As TextInputStream Dim rowFromFile As String Dim count As Integer =0 f=GetOpenFolderItem("text/plain") //defined as a FileType If f <> Nil Then textInput =TextInputStream.Open(f) textInput.Encoding=Encodings.UTF8 Do if instr(0,textInput.ReadLine, "BJLive/Default.aspx?") > 0 then count = count + 1 end if Loop Until textInput.EOF textInput.Close End If

However, with this snippet and the use of instr() it is assumed, that the search string only occurs max once per line. You might need to adjust it if happens to be more times in one line.

Alex thanks so much for your help starting to make sense. :slight_smile:

Can anyone help a little further please. I have a text label that needs to display the total count value.

Thanks

:slight_smile:

Label1.text = Str(count)

Thanks Wayne LOL how did i miss that… :slight_smile:

Almost there :slight_smile: Just one more thing to work out.

I have the following code in a timer event running every 5 minutes.

[code]

dim d As New Date
dim datetime as string = str(d.year) + “-” + str(d.Month) + “-” + str(d.day) + " " +str(d.hour) + “:” + str(d.minute)
TextField1.text = datetime

dim datetimesearch as string = str(d.year) + “-” + str(d.Month) + “-” + str(d.day) + " " +str(d.hour) + “:” + str(d.minute - 5)
TextField2.Text = datetimesearch

Dim f As FolderItem
Dim textInput As TextInputStream
Dim rowFromFile As String
Dim count As Integer =0

f = GetFolderItem(“c:\ex130725.log”)
If f <> Nil Then
textInput =TextInputStream.Open(f)
textInput.Encoding=Encodings.UTF8
Do
if instr(0,textInput.ReadLine, “BJLive/Default.aspx?”) > 0 then
count = count + 1
Label1.text = Str(count)
end if
Loop Until textInput.EOF
textInput.Close
End If[/code]

So basically what i am trying to achieve is to read the log file and make a count of how many times an HTTP request has been made for webpage “BJLIVE/Default.aspx?” within last 5 minute intervals. (Getting the system date+time now minus 5 minutes)

Here is one line from the log file
2013-07-25 01:22:50 W3SVC1 CHI-PROD-LTW-1 172.24.50.30 GET /bjlive/Default.aspx

Thanks I really appreciate help given with this.

:slight_smile:

So what is your exact question?

Hi Alex just trying to work out how to read the first line in the log file get the date and time from the first line and return only the total count of instr(0,textInput.ReadLine, “BJLive/Default.aspx?” for the previous 5 minutes

2013-07-25 01:22:50 W3SVC1 CHI-PROD-LTW-1 172.24.50.30 GET /bjlive/Default.aspx // First Line Entry
evrerything else in between
2013-07-25 01:17:50 W3SVC1 CHI-PROD-LTW-1 172.24.50.30 GET /bjlive/Default.aspx // Last Entry up to a 5 minute period

This technique is new to me in Xojo

I presume that I may need to use some regular expression first and then parse the results.

MID(Logline, 12,8)

Like Richard said - have a look at the Mid() function and the Date class

Thanks for your help Alex and Richard. Very much appreciated :slight_smile:

Still learning on the Job. Xojo is awesome.

The date and time in your file is in SQLDateTime format, so you can create a new Date object and assign that portion of the string to it directly. Then you can use the TotalSeconds property to determine if it is in range.

dim now as new Date dim logdate as new Date logdate.SQLDateTime = left(stringFromTheFile, 19) if now.TotalSeconds - logdate.TotalSeconds < 300 then // happened in the last 5 minutes end

Hi Tim,

???

It’s a text file just a IIS log file

Yes, it’s a text file but the displayed format of the date/time matches SQLDateTime text format.

In any case I wouldn’t use the date/time. Depending on the traffic that’s not granular enough. Save the file position (ie. line count) from the previous run and move forward. You’re going to have to keep some data off to the side anyway since the file name will change every day usually at midnight local time or GMT depending on the configuration, so you’re going to need to parse the end of one file and the beginning of the next each day.

:slight_smile: thanks for all your help. Been in hospital so sorry for late response. Have some catching up to do.

How would subract 5 minutes from SQLDatetime?

dim d As New Date dim datetime as string = d.SQLDateTime

Thanks :slight_smile:

Have a look at Totalseconds :slight_smile:

Thanks Alex

d.Totalseconds = d.Totalseconds - 300 

:slight_smile: