views:

1071

answers:

9

Howdy,

Where would you write an ErrorLog.txt file in Windows? Keep in mind the path would need to be open to basic users for file write permissions.

I know the eventlog is a possible location for writing errors, but does it work for "user" level permissions?

Thank you,
Keith

EDIT: I am targeting Windows 2003, but I was posing the question in such a way as to have a "General Guideline" for where to write error logs.
As for the EventLog, I have had issues before in an ASP.Net app where I wanted to log to the Windows EventLog, but had security issues causing me heartache. (I do not recall the issues I had, but remember having them)

A: 

%TEMP% is always a good location for logs I find.

workmad3
If the logs are relatively unimportant long term, I would agree Temp is the right thing.
Jared Updike
A: 

Put it in the directory of the application. The users will need access to the folder to run and execute the application, and you can check write access on application startup.

The event log is a pia to use for troubleshooting, but you should still post significant errors there.

EDIT - You should look into the MS Application Blocks for logging if you are using .NET. They really make life easy.

Jeez Karma-killers. Next time I wont even offer a suggestion when the poster puts up an incomplete post.

StingyJack
Application logs should not be placed in the application directory. In a secure environment, non administrators will not have write access.
Greg D
Putting a log file in the directory the app is installed in is a *BAD* *BAD* idea. I can't reiterate that strongly enough. A user on Vista may be able to run an app, but they WON'T be able (and should NOT be able) to write to the app directory in %PROGRAMFILES%
Rob
Not an option for programs under vista, and a bad idea in general. Vista will not allow the program to create files in 'Program Files' so if the app is installed there the logging will fail drastically.
workmad3
He doesnt seem comfortable or familiar with the event log, and it <i>is</i> a pain to use for troubleshooting. I dont see why my suggestion merits a -2, especially since the poster seems to not have a clue (and didnt provide one) about the environment.
StingyJack
Who uses vista??? =)None of you has ever made a /log folder and put rolling logs in there?
StingyJack
Nope, I've never dropped a /log in my app folder. I've had to troubleshoot far too many applications that do. :)
Greg D
I've been on the server end (where this works just fine) too long...
StingyJack
Andrew - it merits a "-2" because writing to the application folder is, if you'll pardon my bluntness, a damn stupid idea. There is nothing wrong with log files, BUT, the program folder is the worst place to put them.
Rob
Personally, once a wrong answer has a negative, I don't pile on. As soon as I realize one of my answers is not correct and gets -1, I delete it, because of the SO pile on effect. It's not even worth correcting it--people think the negative means that it's wrong and will still keep downvoting it.
Lou Franco
@[Lou Franco]: like sharks and piranhas!
Steven A. Lowe
I pile away because I think it's wrong too. "One man, one vote" and all. It must be extremely wrong. Although, @Rob, I can think of worse places to put a log file.. system32?
Aidan Ryan
+12  A: 

Have you considered logging the event viewer instead? If you want to write your own log, I suggest the users local app setting directory. Make a product directory under there. It's different on different version of Windows.

On Vista, you cannot put files like this under c:\program files. You will run into a lot of problems with it.

In .NET, you can find out this folder with this:

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

And the Event Log is fairly simple to use too:

http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog(VS.71).aspx

Lou Franco
The app setting directory is a good location. Easily retrieved in .net WinForms, without extra munging, via static properties on your Application object (.CommonAppDataPath, .LocalUserAppDataPath, etc.)
Greg D
The problem with using the app setting folder is that most of the time log files are opened manually in notepad for Explorer, which means that the user is going to have to go digging for it.
James Curran
As a rule, I provide a mechanism via the UI to directly open the log file. And your software always works, so when are they going to need to look at the log, right? ;)
Greg D
+3  A: 

Personally, i would suggest using the Windows Event Log, it's great. If you can't, then write the file to the ApplicationData directory or the ProgramData (Application Data for all users on xp) directory.

Darren Kopp
+1  A: 

The standard location(s) are:

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

or

C:\Documents and Settings\%Username%\Application Data\MyApp

(aka %UserProfile%\Application Data\MyApp) which would match your user level permission requirement. It also separates logs created by different users.

Using .NET runtime, these can be built as:

AppDir=
  System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

or

AppDir=
  System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

followed by:

MyAppDir = IO.Path.Combine(AppDir,'MyApp')

(Which, hopefully, maps Vista profiles too).

gimel
This isn't right for Vista -- (better to use .NET Environment class, or something that wraps the right call in whatever language you are using)
Lou Franco
+2  A: 

The Windows Event Log is definately the way to go for logging of errors. You're not limited to the "Application" log as it's possible to create a new log target (e.g. "My Application"). That may need to be done as part of setup as I'm not sure if it requires administrative privileges or not. There's a Microsoft example in C# at http://support.microsoft.com/kb/307024.

Windows 2008 also has Event Log Forwarding which can be quite handy with server applications.

Rob
Create a new "Event Source" (text in one field) in an existing log requires admin permissions, so I'd have to assume the creating a new Log (an entirely new file) would also require admin rights.
James Curran
This was the "issue" I had before, Admin rights required for the web app to create the log in the Windows Event Log.
Keith Sirmons
A: 

Going against the grain here - it depends on what you need to do. Sometimes you need to manipulate the results, so log.txt is the way to go. It's simple, mutable, and easy to search.

Take an example from Joel. Fogbugz will send a log / dump of error messages via http to their server. You could do the same and not have to worry about the user's access rights on their drive.

David Robbins
+3  A: 

Text files are great for a server application (you did say Windows 2003). You should have a separate log file for each server application, the location is really a matter of convention to agree with administrators. E.g. for ASP.NET apps I've often seen them placed on a separate disk from the application under a folder structure that mimics the virtual directory structure.

For client apps, one disadvantage of text files is that a user may start multiple copies of your application (unless you've taken specific steps to prevent this). So you have the problem of contention if multiple instances attempt to write to the same log file. For this reason I would always prefer the Windows Event Log for client apps. One caveat is that you need to be an administrator to create an event log - this can be done e.g. by the setup package.

If you do use a file, I'd suggest using the folder Environment.SpecialFolder.LocalApplicationData rather than SpecialFolder.ApplicationData as suggested by others. LocalApplicationData is on the local disk: you don't want network problems to stop you from logging when the user has a roaming profile. For a WinForms application, use Application.LocalUserAppDataPath.

In either case, I would use a configuration file to decide where to log, so that you can easily change it. E.g. if you use Log4Net or a similar framework, you can easily configure whether to log to a text file, event log, both or elsewhere (e.g. a database) without changing your app.

Joe
A: 

I agree with Lou on this, but I prefer to set this up in a config file like Joe said. You can use

file value="${APPDATA}/Test/log-file.txt"

("Test" could be whatever you want, or removed entirely) in the config file, which causes the log file to be written to "/Documents and Settings/LoginUser/Application Data/Test" on XP and to "/Users/LoginUser/AppData/Roaming/Test on Vista.

Just adding this as I just spent way too much time figuring how to make this work on Vista...

This works as-is with windows apps. To use logging in web applications, I found Phil Haack's blog entry on this to be a great resource: http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx

related questions