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
1#include <stdio.h>
2#include <string.h>
3
4int main(int argc, char **argsch)
5{
6 FILE *stream;
7 char line[10000];
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

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