I was recently working on a project which needed to send daily reports to a vendor
via a web service. As I was building the application, it became apparent that
the source of the data was date-dependent and that recreating a previous day's
report was non-trivial. In my mind, this meant that we absolutely needed some sort of
log file. That way, as long as we were able to get the data to begin with, even
if an error occurred in the transmission or something happened on the vendor's end,
we'd know what information was involved and would be able to easily recreate the transaction.
As an aside, I've always been a fan of using log files for this type of stand-alone
application. Sure you could log the information to a database, but that adds
additional complexity and more chance for error. Log files are simple to implement,
easy to read, and relatively hard to break. After all, if your server's drives
aren't working, then most likely your application is down which means there's
nothing to log anyway!
This brings me to the point of this tip. I've seen lots of other developers do this
same type of thing, but the thing that always stumps me is the file naming schemes
some of them come up with. Sure you might want to add a prefix or suffix to the file, but
if you're creating a daily log file, it only makes sense to name it after the date
it was created. Furthermore, why do people insist on using month, day, year? For example,
using "04152008" for April 15, 2008. Sure it works great for the first few months, but
next year when you go to look for a log file, the one for April 15, 2009 will be right next to it.
Does it really make sense to have two files, that were created a year apart, right next to each other?
Why not use a naming scheme that when sorted numerically puts the files in the appropriate
chronological order?
The format I've been using for some time is quite simple: April 15, 2008 is "20080415". It
goes from largest unit to smallest unit so that all transactions for the same year, quarter, month,
or week are all grouped together. You can add separator characters if you like ("2008-04-15"),
but I think you'll find it's probably not even necessary. Once you get used to the format and
thinking from largest date unit to smallest, it just seems to make sense.
So the question then becomes, how do you construct the file name? Well it's simpler then
you'd think. In .NET, to get today's date in the format described, simply use the Format function: