异常日志文件的最佳位置(Windows)

时间:2023-01-12 23:49:24

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,因此日志记录的位置和类型是完全可配置的。但是,我希望有一个合理的默认值。我更喜欢在事件日志上有一个文件。对于大多数用户而言,文件比事件日志更容易处理。但是,我们假设我想拥有文件。在安装文件夹中有一个日志文件夹可以吗?

7 个解决方案

#1


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.

使用较新的OS vista和win 2008写入程序文件目录可能需要ACL或虚拟化到用户的文件夹。我建议在%AllUsersProfile%下设置一个目录,这样你就可以为所有用户的日志文件提供一个公共位置,而你的应用程序不需要以管理员身份运行就可以在那里写入。

#2


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.

这样做的好处是,当事情不顺利时,地球上几乎每个技术人员(和程序员)都希望在事件日志中显示某些内容。

#3


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下。您需要在某个时候验证该文件夹是否存在 - 可能是在应用程序启动时。

#4


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.

如果您不能或不会使用事件日志,那么实际上没有任何关于将日志文件放在何处的标准化。您只需要确保该位置记录良好,并且确实需要在代码中使用逻辑,以确保日志文件可以增长到的大小有限制。

#5


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应用程序。没做什么。

#6


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和前进中,默认情况下您可能无权写入安装文件夹。

#7


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)并在每个用户的app文件夹中创建子文件夹。通过这种方式,您不仅可以摆脱权限问题,还可以避免混乱自己的空间:)恕我直言。

#1


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.

使用较新的OS vista和win 2008写入程序文件目录可能需要ACL或虚拟化到用户的文件夹。我建议在%AllUsersProfile%下设置一个目录,这样你就可以为所有用户的日志文件提供一个公共位置,而你的应用程序不需要以管理员身份运行就可以在那里写入。

#2


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.

这样做的好处是,当事情不顺利时,地球上几乎每个技术人员(和程序员)都希望在事件日志中显示某些内容。

#3


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下。您需要在某个时候验证该文件夹是否存在 - 可能是在应用程序启动时。

#4


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.

如果您不能或不会使用事件日志,那么实际上没有任何关于将日志文件放在何处的标准化。您只需要确保该位置记录良好,并且确实需要在代码中使用逻辑,以确保日志文件可以增长到的大小有限制。

#5


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应用程序。没做什么。

#6


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和前进中,默认情况下您可能无权写入安装文件夹。

#7


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)并在每个用户的app文件夹中创建子文件夹。通过这种方式,您不仅可以摆脱权限问题,还可以避免混乱自己的空间:)恕我直言。