异常日志文件的最佳位置 (Windows)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/465785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best location for exception log files (Windows)
提问by user57474
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?
异常日志应该放在何处的问题已经在这里讨论过一两次(或多次),其中一项建议是应用程序永远不要写入安装文件夹。
但是,如果我将日志放在 %appdata% 中的某个位置,这意味着每个用户都有自己的一组日志。我更喜欢将所有日志放在一个位置。
在最近的 MSDN mag 问题之一中,在安装文件夹(例如 %programfiles\myapp\logs)中有一个单独的日志子文件夹被称为规则的有效例外。当然,必须相应地设置此文件夹的 ACL。
在安装文件夹中有一个日志文件夹是常见的做法还是绝对不行?你把异常日志放在哪里?
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?
编辑:
实际上,我们使用的是 log4net,因此日志记录的位置和类型是完全可配置的。但是,我想要一个合理的默认值。我更喜欢在事件日志上有一个文件。对于大多数用户来说,文件比事件日志更容易处理。
但是,让我们假设我想要文件。安装文件夹中有一个日志文件夹可以吗?
采纳答案by Aaron Fischer
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.
使用较新的操作系统 vista 和 win 2008 写入程序文件目录可能需要 ACL 或虚拟化到用户文件夹。我建议在 %AllUsersProfile% 下设置一个目录,这样您就可以为所有用户的日志文件提供一个公共位置,并且您的应用程序无需以管理员身份运行即可将它们写入那里。
回答by NotMe
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.
对于 Windows,请使用事件日志。创建您自己的日志非常容易,或者您可以将您的消息添加到标准日志之一。
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.
这样做的好处是,地球上几乎所有技术人员(和程序员)都希望在事情不正常时在事件日志中显示某些内容。
回答by Mike
For logging to a common file without special ACLs, use something like
要登录到没有特殊 ACL 的公共文件,请使用类似
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
在 XP 上,以及类似的东西
C:\ProgramData\MyCompany
under Vista. You'll need to verify that the folder exists at some point - perhaps at application startup.
在 Vista 下。您需要在某个时间点验证该文件夹是否存在 - 可能是在应用程序启动时。
回答by EBGreen
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.
如果您不能或不会使用事件日志,那么对于放置日志文件的位置确实没有任何标准化。您只需要确保该位置记录良好,并且您的代码中确实需要有逻辑以确保日志文件可以增长到的大小有限制。
回答by chills42
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.
这是使用应用程序数据文件夹而不是安装文件夹的关键原因之一,因为在 Vista 和未来,默认情况下您可能无法写入安装文件夹。
回答by Dead account
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.
对于网站,我会在服务器上有一个“C:\log”,然后是单个网站的“C:\log\website”,并使用 Log4Net 或类似的。
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)
对于 Web 服务、窗口服务、计划作业等,我会写入 Windows 事件日志。我的电脑 -> 管理 -> 系统工具 -> 事件视图 ->(我的服务)
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.
对于部署在我控制的环境中的 Windows 应用程序(即公司内部应用程序),我希望它通过电子邮件发送“IT 团队”异常报告。
For a windows application which is distrobuted to unknown users. Do nothing.
对于分发给未知用户的 Windows 应用程序。没做什么。
回答by Gripsoft
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.
我认为您应该更喜欢 AppData 文件夹,但如果您想将所有日志合并到一个文件夹中,请使用所有用户目录 (Environment.SpecialFolder.CommonApplicationData) 并在您的应用程序文件夹中为每个用户创建子文件夹。通过这种方式,您不仅可以摆脱权限问题,还可以避免弄乱自己的空间:) 恕我直言。