Where are the best locations to write an error log

2019-03-13 02:48发布

Where would you write an error log file, say ErrorLog.txt, 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?

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 application where I wanted to log to the Windows event log, but I had security issues causing me heartache. (I do not recall the issues I had, but remember having them.)

10条回答
beautiful°
2楼-- · 2019-03-13 03:20

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).

查看更多
乱世女痞
3楼-- · 2019-03-13 03:22

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 Windows XP) directory.

查看更多
不美不萌又怎样
4楼-- · 2019-03-13 03:24

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

查看更多
我命由我不由天
5楼-- · 2019-03-13 03:26

I agree with Lou on this, but I prefer to set this up in a configuration 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 configuration file, which causes the log file to be written to "/Documents and Settings/LoginUser/Application Data/Test" on Windows XP and to "/Users/LoginUser/AppData/Roaming/Test on Windows Vista.

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

This works as-is with Windows applications. 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

查看更多
登录 后发表回答