I have an IIS log file. And now I would need to make a C# app which will read the log file and show hits for each page. The file looks like this:

2006-04-01 00:27:12 W3SVC1 WEBPORTAL /images/menu/menu_cap_left.jpg - 34 -;+MSIE+6.0;+Windows+NT+5.1;+SV1;+.NET+CLR+1.1.4322) http://sitename/ 200 0 0 222 234
The application will show page hits for each page it finds in the log file. And it will remove hits from the same user.
Does anybody have an idea how this could be done? I do not have much time.
Who is Participating?
Check out this code snippet...

Simple log parsing using MS Log Parser 2.2, in C#.NET
Here is my idea: Write a C# app to sort/copy the log file into the desired sequence.  Read the information.  Count.  Drop the repeats.  Write counted record.  When all counted, written, replace the original log file with the new one, or not.  If you replace you might want to sort it back to the original sequence.
mljackoAuthor Commented:
Hmm, ok, I have this log file, then I have to search line by line and check if there is a particual page in that line. If it is I add the line to an array and then check if there are duplicates. The thing is that I have no idea how to write a regular expression for this.
For example if there is this line:

2006-04-02 03:37:06 W3SVC1 WEBPORTAL GET /images/corners/top_right.gif - 80 - Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1) http://SITENAME/Login.aspx?ReturnUrl=/ 222 0 0 242 233

How do I check if there is a http://SITENAME
and if there is a Login.aspx

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

I'm not a C# expert.  If I recall it does not have an 'instr' function(?) but it must support string indexing of some sort so you should be able to write an equivalent function, or find one already written, or something.  Bottom line appears to be that you will have to parse the record/string to isolate the piece of information that you want to work with.
String class has a split function, so you could split it on spaces and the parse the array.  Something like this:

   string[] lineTokens=line.Split(new char[] {','})
lineTokens[0] would be date
lineTokens[1] would be time
lineTokens[4] would be is address
lineTokens[5] would be method
lineTokens[6] would be url
lineTokens[7] would be port?
lineTokens[8] would be client ip?
lineTokens[9] would be client browser?
lineTokens[10] would be URL page

If the log does NOT have the same number of tokens on each line, then you would have to search for patterns:

   int urlPos=instr.LastIndexOf("http://SITENAME");
   string urlPage=line.SubString(urlPos, line.IndexOf(' ', urlPos));  //Return last string starting with http://SITEName and ending with space
mljackoAuthor Commented:
Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.