在 Windows 中写入错误日志的最佳位置在哪里?
在 Windows 中,您会在哪里写入错误日志文件(例如 ErrorLog.txt
)? 请记住,该路径需要向基本用户开放以获得文件写入权限。
我知道事件日志可能是写入错误的位置,但它适用于“用户”级别权限吗?
编辑:我的目标是 Windows 2003,但我提出问题的方式是为在哪里写入错误日志提供“一般指南”。
至于EventLog,我之前在一个ASP.NET应用程序中遇到过问题,我想记录到Windows事件日志,但我遇到了让我心痛的安全问题。 (我不记得遇到过什么问题,但记得有过这些问题。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
%TEMP% 始终是我找到的日志的好位置。
%TEMP% is always a good location for logs I find.
这里违背了规律——这取决于你需要做什么。 有时您需要操作结果,因此 log.txt 是最佳选择。 它简单、可变且易于搜索。
以乔尔为例。 Fogbugz 将通过 http 向其服务器发送错误消息日志/转储。 您可以执行相同的操作,而不必担心用户对其驱动器的访问权限。
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.
我个人不喜欢使用我现在所在的 Windows 事件日志,因为我们无权访问生产服务器,因此这意味着我们每次想要查看错误时都需要请求访问权限。 不幸的是,这不是一个快速的过程,因此您的故障排除完全是通过等待其他人来完成的。 我也不喜欢它们在其他应用程序中迷失方向。 当然你可以排序,但是向下滚动有点细微的差别。 您使用的内容最终将是个人偏好与您工作环境的限制的结合。(日志文件、事件日志或数据库)
I personally don't like to use the Windows Event Log where I am right now because we do not have access to the production servers, so that would mean that we would need to request access every time we wanted to look at the errors. It is not a speedy process unfortunately, so your troubleshooting is completely haulted by waiting for someone else. I also don't like that they kind of get lost within the ones from other applications. Sure you can sort, but it's just a bit of a nucance scrolling down. What you use will end up being a combination of personal preference coupled along with limitations of the enviroment you are working in. (log file, event log, or database)
将其放在应用程序的目录中。 用户将需要访问该文件夹才能运行和执行应用程序,并且您可以在应用程序启动时检查写入权限。
使用事件日志进行故障排除很麻烦,但您仍然应该在那里发布重大错误。
编辑 - 如果您使用 .NET,您应该查看 MS 应用程序块进行日志记录。 他们确实让生活变得轻松。
天哪,业力杀手。 下次当发帖者发布不完整的帖子时,我什至不会提供建议。
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 pain 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 won't even offer a suggestion when the poster puts up an incomplete post.
您是否考虑过记录事件查看器? 如果您想编写自己的日志,我建议用户本地应用程序设置目录。 在那里创建一个产品目录。 不同版本的 Windows 上有所不同。
在 Vista 上,您不能将此类文件放在 c:\program files 下。 你会遇到很多问题。
在 .NET 中,您可以通过以下方式找到此文件夹:
事件日志的使用也相当简单:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx
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:
And the Event Log is fairly simple to use too:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx
文本文件非常适合服务器应用程序(您确实说过 Windows 2003)。 您应该为每个服务器应用程序拥有一个单独的日志文件,该位置实际上是与管理员达成一致的约定问题。 例如,对于 ASP.NET 应用程序,我经常看到它们放置在与应用程序不同的磁盘上,位于模仿虚拟目录结构的文件夹结构下。
对于客户端应用程序,文本文件的一个缺点是用户可能会启动应用程序的多个副本(除非您已采取特定步骤来防止这种情况发生)。 因此,如果多个实例尝试写入同一个日志文件,就会出现争用问题。 因此,我始终更喜欢客户端应用程序的 Windows 事件日志。 需要注意的是,您需要成为管理员才能创建事件日志 - 这可以通过安装包来完成。
如果您确实使用文件,我建议使用文件夹Environment.SpecialFolder.LocalApplicationData,而不是其他人建议的SpecialFolder.ApplicationData。 LocalApplicationData 位于本地磁盘上:当用户具有漫游配置文件时,您不希望网络问题阻止您进行日志记录。 对于 WinForms 应用程序,请使用 Application.LocalUserAppDataPath。
无论哪种情况,我都会使用配置文件来决定记录位置,以便您可以轻松更改它。 例如,如果您使用 Log4Net 或类似的框架,您可以轻松配置是否记录到文本文件、事件日志、两者或其他地方(例如数据库),而无需更改您的应用程序。
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.
标准位置是:
或
(又名
%UserProfile%\Application Data\MyApp
),它符合您的用户级别权限要求。 它还分隔不同用户创建的日志。使用.NET运行时,可以将它们构建为:
或
后跟:(
希望这也可以映射Vista配置文件)。
The standard location(s) are:
or
(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:
or
followed by:
(Which, hopefully, maps Vista profiles too).
就我个人而言,我建议使用Windows事件日志,它很棒。 如果不能,则将该文件写入 ApplicationData 目录或 ProgramData(适用于 Windows XP 上所有用户的应用程序数据)目录。
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.
Windows 事件日志绝对是记录错误的最佳方式。 您不限于“应用程序”日志,因为可以创建新的日志目标(例如“我的应用程序”)。 这可能需要作为设置的一部分来完成,因为我不确定它是否需要管理权限。 有一个 Microsoft C# 示例,位于 http://support.microsoft.com/kb/307024 。
Windows 2008 还具有事件日志转发,这对于服务器来说非常方便应用程序。
The Windows event log is definitely 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.
我同意 Lou 的观点,但我更喜欢像 Joe 所说的那样在配置文件中进行设置。 您可以在配置文件中使用
file value="${APPDATA}/Test/log-file.txt"
(“Test”可以是您想要的任何内容,也可以完全删除),这会导致日志文件写入“/文档和设置/登录用户/应用程序
在 Windows XP 上为“Data/Test”,在 Windows Vista 上为“/Users/LoginUser/AppData/Roaming/Test”。
我只是添加这个,因为我花了太多时间考虑如何使其在 Windows Vista 上工作......
这与 Windows 应用程序一样工作。 要在 Web 应用程序中使用日志记录,我发现 Phil Haack 的博客文章是一个很好的资源:
http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications。 ASPX
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