views:

771

answers:

7

The question where exception logs should go has been discussed here once or twice (or more), and one of the recommendation was that the application should never write to the installation folder.
However, if I put the logs somewhere in %appdata%, this means that each user has its own set of logs. I'd prefer having all logs at a single location.
In one of the last MSDN mag issues, having a separate sub folder for logs in the installation folder (e.g. %programfiles\myapp\logs) was called a valid exception from the rule. Of course, the ACL for this folder must be set up accordingly.
Is having a log folder in the installation folder common practice or an absolute no-go? Where do you put your exception logs?

Edit:
In fact, we are using log4net, so the location and type of logging is completely configurable. However, I want to have a reasonable default. I prefer having a file over the event log. For most users, a file is much easier to handle than the event log.
However, let's assume that I want to have files. Is it okay to have a log folder in the installation folder?

+7  A: 

For windows, use the event log. It's pretty easy to create your own log or you can just add your messages to one of the standard ones.

This has the benefit that nearly every tech (and programmer) on the planet expects something to show up in the event log when things aren't going right.

Chris Lively
Really? Looking at `eventvwr` has never been my first intuition when something crashes :P I just compared my `%AllUserProfile%` and saw everybody using it. OTOH I can't find any logs except MS created ones in `eventvwr`. Besides, writing log as files instead of Windows event make it easier for the logging part to be cross-platform and portable.
kizzx2
@kizzx2: Depending on the application we either log to the standard "Application" log or have the app create our own. I just looked in mine and found reference to nearly 200 different apps/services using it. My %AllUserProfile% directory seems to be used by some applications to store their config data.
Chris Lively
Oh, and I forgot to mention: centralized management systems can query and monitor remote event logs. Which is big plus for larger environments.
Chris Lively
+1  A: 

It depends mainly on the application, which is why I guess there are so many possible answers.

For websites, I would have a "C:\log" on the server, then "C:\log\website" for individual websites and use Log4Net or similar.

For web services, window services, scheduled jobs etc, I would write to the Windows Event Log. My Computer -> Manage -> System Tools -> Event View -> (My Sevice)

For a windows application which is deployed in an environment I control (i.e. internal company application) I woudl have it email "the IT team" exception reports.

For a windows application which is distrobuted to unknown users. Do nothing.

Dead account
+2  A: 

If you cannot or won't use the event logs, then there really isn't any standardization on where to put the log file. You just need to be sure that the location is well documented and there really needs to be logic in your code to be sure that there is a limit on the size that the log file can grow to.

EBGreen
+1  A: 

You'll want to watch out for permissions to write to the log folder.

This is one of the key reasons to use the application data folder instead of the install folder, because in Vista and moving forward, by default you probably won't have access to write to the install folder.

chills42
+4  A: 

With the newer OS vista and win 2008 writing to the program files directory may need ACL or be virtualized to the user's folder. I would suggest setting up a directory under %AllUsersProfile% this way you have one common location for all user's log files and your applications does not need to run as admin to write them there.

Aaron Fischer
+1  A: 

I think You should prefer the AppData folder but if you want to combine all the logs in one folder then use All Users Directory ( Environment.SpecialFolder.CommonApplicationData) and create sub folders inside your app folder for each user. In this way you not only get rid of permission issues but also avoid cluttering your own space :) IMHO.

Gripsoft
+2  A: 

For logging to a common file without special ACLs, use something like

string saveFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\MyCompany";

which references the folder in

C:\Documents and Settings\All Users\Application Data\MyCompany

on XP, and something like

C:\ProgramData\MyCompany

under Vista. You'll need to verify that the folder exists at some point - perhaps at application startup.

Mike