Posted on 2006-04-04
Last Modified: 2008-02-01
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.
Question by:mljacko
    LVL 4

    Expert Comment

    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.

    Author Comment

    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

    LVL 4

    Expert Comment

    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.
    LVL 21

    Expert Comment

    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
    LVL 10

    Accepted Solution

    Check out this code snippet...

    Simple log parsing using MS Log Parser 2.2, in C#.NET

    Author Comment

    Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    character entities and entity references in XML 17 81
    count8 challlenge 13 67
    firstChar challenge 13 56
    python sqlite question 11 21
    A short article about problems I had with the new location API and permissions in Marshmallow
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now