IIS7 logging in Central Location for web farms







Ok so it appears IIS7 has thought of almost everything this time, and IIS7 is perfect for web Farms now they have the ability to share the configuration file.

While we are in the mists of setting up a load balanced environment with a IIS7 farm, we have everything working wonders. But when it comes to log files it seems Microsoft missed this. they really should have added in the functionality to combine log files to a central location, while we can prove a UNC path and also log to this, but only one server can do this.

the problems that you are faced with in a web farm is that:

If you have shared you ApplicationHost.config which in a web farm, (I highly recommend you do to make your life that little be easier) you will find that y our log file location will be the same on all servers. which could be a problem if:

you have used a UNC path, all servers in the farm will try and log to this location and only one can win, as the HTTP.SYS for that winning server will keep this open and therefore other servers will not be able to log.

If you have specified a local path, which really lest face it is your only option, you MUST make 100% sure that location or that drive exists on all of your servers in the web farm.

Now if you don’t care so much for your log files you will be happy to have them on each server and not collate them. But for the people like us we need them in a central location for a: We like to find our data quickly and be able to find what is going on, on our servers b; we have webstats that we need to be able to provide to our clients or internal staff and c; for a backup / management purpose its just easier to have them centralised.

So our requirement is to be able to copy all of our log files to one location, keeping of course the web log file structure i.e. W3SVC1, W3SVC2 etc…

there is one other problem I should tell you, all of the log file names are the same name, so we can just schedule a move / copy script because they will fail or overwrite the other log files, you MUST rename the files before moving. In hind sight we have do this but its a good idea anyway, because this will allow us to easily track down a problem on a server via the logs.

So we have come up with a script that can be scheduled on a daily basis that will rename all files to the server name_<orginalfilename>.log (this will look in all sub folders i.e. W3SVC1, W3SVC2 etc…) once the name change is done it will then MOVE the files to the central location.

A Big thanks to Alan Lee for this script, as he wrote the find and replace part (hats off)

set logs=C:\folder\folderagain
cd /d %logs%
dir /b %logs% > tmp.txt
for /D %%I in ("%logs%") do For /F "tokens=*"  %%J in (tmp.txt) do cd /d %logs%\%%J && for %%i in (u*.log) do move %%i %computername%_%%i 
del %logs%\tmp.txt
robocopy %logs% \\servername\FolderName /mov /E /minage:0 /R:3 /W:5 /LOG:%logs%\logs.log

Of course the script uses robocopy so you will need to include the .exe for this where ever you are executing this .bat from.

With 2008 Server you can also schedule emails with attachments, might not be a bad idea to attach the log file so you can archive it away.

12 Responses to “IIS7 logging in Central Location for web farms”

Leave a Reply