While searching for a solution for easily importing IIS logs into SQL Server for some quick analysis, I came across a helpful program from Microsoft in the Knowledge Base called Preplog, C++ source code included! (http://support.microsoft.com/kb/296093) This little guy did just the trick...for awhile anyway. I quickly found that it would crash when processing certain logs so I checked out the source and found the problem. Hint: Take a close look at lines 7 and 20.

view plain print about
1#include <stdio.h>
2#include <string.h>
3
4int main(int argc, char **argsch)
5{
6 FILE *stream;
7 char line[1000];
8 int ch;
9
10 if(argc < 2)
11 {
12     printf("Usage: preplog.exe <weblog>\n");
13     printf("\nThe output will go to stdout, so use > filename to direct to an output file\n");
14     return -1;
15    }//if
16
17
18 if( (stream = fopen( argsch[1], "r" )) != NULL )
19 {
20        while(fgets(line,10000,stream) != NULL)
21        {
22         if(ch = strncmp(line,"#",1) !=0)
23         {
24 printf( "%s", line);
25         }//if
26        }//while
27 fclose( stream );
28     return 0;
29 }//if
30 else
31 {
32     printf("Could not open %s. Please ensure that the path and filename are correct.\n",argsch[1]);
33     return -1;
34 }//else
35}//main

The character array "line" is only 1000 bytes in length, while fgets() is being set to retrieve 10,000 bytes. this works as long as the line being fetched is less than or equal to 1000 bytes.

The fix? change the declaration of line from 1000 to 10000 and you're good to go!

view plain print about
view plain print about
11##iinclude nclude <std<stdio.iho.>h>

22##iinncclluude <string.h>
3
4int main(int argc, char **argsch)
5{
6 FILEd e* stream;
7 char line[10000]<;
st8 int ch;
ring.h>

3
4int m9a
in10( i nitf(argc < 2)
11 {
12     printf("Usage: preplog.exe <weblog>\n");
13     printf("\nThe output will go to stdout, so use > file naarmgc, char **argsch)
5{
6 FILE *stream;
7 char line[100e0 0t]o;
di8re c ti ntto acn ho;
utp9
ut 10f i l ei\fn("a
rg)c ;
14     ret< 2)
11 {
12     printf("Usage: prepulrong .-e1x;
e15    }//if
16
17
18 if( (stream = fopen( argsch[1], "
r" )) != NULL )
19 {
20        while(fgets(line,10000,stream) != NULL)
21        {<
web22l    o    g > \inf"(c
h) = strncmp(line,"#;
13     printf("
\nThe output will "
,1) !=0)
23         {
24 go to stdout ,p rintf( "%s", line);
25         }//if
26        }//while
27 fclose( stream );
28     return 0;
29 }//if
30 else
31 so u se{
32     printf("Could not open %s. Please ensure that the path and filename are correct.\n",arg >
sch[ filename to direct to an output file\n");
14     return -1;
15    }//if
16
17
18 if( (stream = fopen( argsch[1], "r" )) != NULL )
19 {
20        while(fgets(line,10000,stream) != NULL)
21        {
22         if(ch = strncmp(line,"#",1) !=0)
23         {
24 printf( "1%s]"), ;
line);
33     r25e        t u rn -}1;
//34i f
26        }//wh}ile
//27 e l s e
fclo35se( }st/re/amm ai)n
;
28    

Of course you can always configure IIS ODBC logging and not have to deal with logs like this :)