56

I need to store log files and configuration files for my application. Where is the best place to store them?

Right now, I'm just using the current directory, which ends up putting them in the Program Files directory where my program lives.

The log files will probably be accessed by the user somewhat regularly, so %APPDATA% seems a little hard to get to.

Is a directory under %USERPROFILE%\My Documents the best? It needs to work for all versions of Windows, from 2000 forward.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dave
  • 5,436
  • 11
  • 48
  • 74
  • See also [this question](http://stackoverflow.com/questions/1556082/as-a-developer-how-should-i-use-the-special-folders-in-vista-and-windows-7) – Dimitri C. Aug 17 '10 at 07:12

9 Answers9

40

If you're not using ConfigurationManager to manage your application and user settings, you should be. The configuration toolkit in the .NET Framework is remarkably well thought out, and the Visual Studio tools that interoperate with it are too.

The default behavior of ConfigurationManager puts both invariant (application) and modifiable (user) settings in the right places: the application settings go in the application folder, and the user settings go in System.Environment.SpecialFolder.LocalApplicationData. It works properly under all versions of Windows that support .NET.

As for log files, System.Environment.SpecialFolder.LocalApplicationData is generally the place that you want to put them, because it's guaranteed to be user-writeable.

There are certainly cases where you wouldn't - for instance, if you want to write files to a network share so that you easily can access them remotely. There's a pretty wide range of ways to implement that, but most of them start with creating an application setting that contains the path to the shared folder. All of them involve administration.

I have a couple of complaints about ConfigurationManager and the VS tools: there needs to be better high-level documentation than there is, and better documentation of the VS-generated Settings class. The mechanism by which the app.config file turns into the application configuration file in the target build directory is opaque (and the source of one of the most frequently asked questions of all: "what happened to my connection string?"). And if there's a way of creating settings that don't have default values, I haven't found it.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • 6
    Using System.Environment.SpecialFolder.LocalApplicationData for config settings has one drawback - it does not replicate with the roaming profile, if the user is in a such an environment. And I had bad experience with that, when I had to modify my app later on, when I got users like this. – Sunny Milenov Nov 07 '08 at 15:53
  • Depending on what you are logging as well. If the logs are specific to the users' executions then use Environment.SpecialFolder.LocalApplicationData. If you just logging general application stats then I would recommend using the Environment.SpecialFolder.CommonApplicationData – Tal Even-Tov Mar 27 '13 at 12:24
12

Note: You can get the path to the LocalApplicationData folder in .NET by using the following function:

string strPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
AdamantineWolverine
  • 2,131
  • 1
  • 17
  • 14
9

For application settings - use System.Environment.SpecialFolder.ApplicationData - this is where a roaming profile data is stored, so it allows your user to log and work from different machines in the domain.

For log files - System.Environment.SpecialFolder.LocalApplicationData

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Sunny Milenov
  • 21,990
  • 6
  • 80
  • 106
8

The accepted answer notes that for log files the following is a good spot. System.Environment.SpecialFolder.LocalApplicationData This equates to a path of C:\Users\[User]\AppData\Roaming which you can see is user specific. Like the accepted answer mentions this is a guaranteed user-writeable location and can be useful for certain situations

However in a web application environment you may be running your application under a network account and you or a coworker may need to try and track down where exactly those logs are going per application. I personally like to use the non user specific location enumeration of System.Environment.SpecialFolder.CommonApplicationData which equates to C:\ProgramData. Yes, you will need to specify access rights for any folders you create, but it's usually a one time deal and then all of your application logs can live in one happy location.

Additionally, while looking around the Internet, there is a project out there to programatically set write access to folders you create within CommonApplicationData, Allow write/modify access to CommonApplicationData.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dylan Hayes
  • 2,331
  • 1
  • 23
  • 33
  • 1
    `System.Environment.SpecialFolder.LocalApplicationData` equates to `C:\Users\[User]\AppData\Local`. `System.Environment.SpecialFolder.ApplicationData` equates to `C:\Users\[User]\AppData\Roaming` – AdamF Aug 05 '23 at 16:51
6

To be honest %appdata% is still the best place to place your config files and log files, as it serves the purpose of a placeholder to store your application data. It should not be that hard to access, just write %appdata% in explorer and you will be directed straight to your %appdata% directory.

RWendi
  • 1,446
  • 5
  • 20
  • 38
4

Do not store config files in the application folder, Microsoft has stated this is NOT the ideal location. Windows has been moving towards blocking writing to C:\Program Files\ and you'll find in Vista any application that tries to write here, will fire up a UAC warning.

Windows 7 will allow users to customize what UAC popups they use (expect some power users to block most of them) and your app will fail/freeze if the user never approves this write attempt.

If you use the proper userprofile and appdata variables, then Win 2000, XP, Vista, and Win7 will map the data to the proper write friendly folder, with no UAC popups.

TravisO
  • 9,406
  • 4
  • 36
  • 44
2

You can use SHGetSpecialFolderPath:

int MAX_PATH = 255;

CString m_strMyPath;

SHGetSpecialFolderPath(NULL, m_strMyPath.GetBuffer(MAX_PATH), CSIDL_COMMON_APPDATA, TRUE);

This will specify the 'special folder path' which you can safely write logs to for windows:

For XP: C:\Documents and Settings\All Users\Application Data

For Vista: C:\ProgramData

Check the MSDN page here: http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx

Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
Klathzazt
  • 2,415
  • 19
  • 27
1

The best answer depends on the nature of the logs and configurations. If they are program-wide, and don't need to survive uninstallation of the application, then I think they're fine where they are. If the logs and configurations are user specific, or need to survive uninstallation, then they belong somewhere under %USERPROFILE% - %APPDATA% being the 'proper' base directory for this type of thing.

Harper Shelby
  • 16,475
  • 2
  • 44
  • 51
0

I use the Isolation Storage for configuration. You can also use the Temp folder to store temporary information like log.

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
  • That's your opinion. Log are supposed to be temporary... might depend of your application. – Patrick Desjardins Nov 06 '08 at 19:58
  • 3
    and I'd argue that "Logs are supposed to be temporary" is your opinion as well. – Dylan Hayes Oct 18 '12 at 13:50
  • 2
    and three and a half years later, I come back here, and see this discussion. @DylanHayes is obviously right - log files are especially useful after a program stopped its operation, and there's absolutely no guarantee any files of before that happened will still be in the temporary folder. So no, logs are **not** supposed to be temporary; what you might mean is something like "status info". – Marcus Müller May 09 '16 at 13:34