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
view plain print about
111###iiinnnccllude udclude e <stdio.h><s
tdi2<o#.sith>nd
ico2l.#uhid>n
e
cl2#include <string.h><
st3
ri4ngi.nht>

m3a
i4ni(nitn tm aargc, in(int argc, cchhaarr ****aarrggsscchh))

55{{

66 FFIILLEE **ssttrreeaamm;;
7
c7h a r c hlair nlei[n1e0[010000]0;
0]8;
8i n t i nct h ;
ch9;

109
10i f (iafr(gacr gc < 2)
11 {
12     printf(ud"eU sage: preplog.exe < 2)
11< s t rin{
12     printf("
Ug.h>

s3
age4: ipnrt empaliong(.ienxte argc, char <weblog>\*n"*)a<r;
gwsecb13hl    ) o
g >
5pr\in{"n
)tf;6
( " \FnITLhEe *osuttrpeuat mw;
ill7 g o cthoa rs tldion13u    e [t 1 , 0prs0ion0 t0uf]s(;
"e\8n T hei not u tcphu;
t w9i
ll 10g o if(argc < > f il2e)
nt11o s{t
da12om    ue ttp,or isdniotr fe(uc"stU es taog> e
a: n fpoirulteeppnluaot mgfe.i leetx\oen" )d;i
re14c    t tro etaun ron u-t1p;<weblog>\n");
13     upt file\n");
r14in     t f(r"e\tnuTrhen o-u1tp;
ut 15w    ill }go//if
16
t17o
st18do u ti,f (s o( stream = fopen(u saer g>
s chf[il1e]n, "r" )) != NULL
15    }//if
16
17
18 i)
a19m e to {d
20        whf( irect itlo e(anf goeuttsp(ulti file\n");
(st14r    e a m re= tufronp e-n1( ;
args15ch    [1]}, "r"/ )/if
16
17
18 if( (stream = fopen( argsch[1], "r" )) != NULL) != NULL )
19 {
)
20        19w h ile({fg
ets(20l    i    nweh,i1l0e(0f0g0e,tss(trleianme), 1!0= 0N0U0L,L)s
trea21m    )     != {NU
LL)
22         21i    f    (ch {=
strn22cm        p ( liifn(ec,"h #"= ,s1t)r n!c=mp0()
line23,"        # " ,1) {!
=0)
24 23           prin{tf
( "%s24" , l i n e ) ;
print25f    (     " %s",} lin/e)/;
if
2526                  }}///while
27 fclos/ei( f
strea26m         );
}28/     /rwehtiulren
0;
27 29 fclo}se( /st/rief
am )30;
28     return 0e;
lse
29 }//if
30 else
31 31 {
32     printf("C{ne,10000,stream) != NULL)
21        {
22         if(ch = strncmp(line,"#",1) !=o0u)l
d not 23o    p     e n %s.{
Pleas24e e n s u r e ptrhiant tft(h "e %psa"t,h lainnd e)fi;
lenam25e         a r e cor}rect.\n"/,a/rigsf
ch[1]26)        ;
}33    / /rewthuirlne
-1;
27 34 f close}( str/eam/ e)l;
se
2835     ret}urn /0;
/main
29
}

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