log4net 写入文件。如何开放权限
我很高兴在 XP 计算机上将 log4net 与我的 WPF 程序一起使用,并愉快地使用 fileAppender FileAppender 将日志消息写入 c:\log.txt。一切都很好。但是,它不适用于 Windows 7 计算机。没有错误或任何东西,只是文件没有创建,更不用说记录了。一些研究表明,这是 Windows 7 的文件权限问题 (UAC),事实上,如果我以管理员身份运行可执行文件,它就会起作用。如果我只是单击它,它就不起作用(即使我以管理员身份登录),并且当我从 Visual Studio 启动时,它也不起作用。
问题: 1. 有人能给我举一个例子,我请求写入一个且仅一个文件(C:\log.txt)的权限吗?我见过一些示例,其中 app.config 配置为要求整个程序以管理员权限运行。这看起来有点矫枉过正,但我想它会起作用。 2.是否有更好的方法将信息发送到日志文件?毕竟,也许用户计算机上不存在 C:。我想我记得 Windows 7 中“用户分区”的想法,但无论我做什么都必须在 XP 和 Vista 上运行。
非常感谢, 戴夫
I was happily using log4net with my WPF program on an XP machine and happily using a fileAppender FileAppender to write log messages to c:\log.txt. All was well. However, it does not work on a Windows 7 machine. No error or anything, just that the file isn't created, much less logged to. A little research reveals that it's a file permissions problem (UAC) with Windows 7, and in fact it works if I run the executable as administrator. It doesn't work if I just click on it (even though I'm logged on as administrator) and it doesn't work when I launch from Visual Studio.
Questions:
1. Can someone point me to an example where I ask for permission to write to one and only one file (C:\log.txt). I've seen some examples of where the app.config is configured to ask that the whole program is run with admin privileges. This seems like overkill but I guess it would work.
2. Is there as better way to send the information to a log file? After all, perhaps C: does not exist on user machine. I think I recall the idea of a "user partition" in Windows 7, but whatever I do has to work on XP and Vista.
Thanks a ton,
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该尝试直接写入根文件夹。在 Windows 7 下,您要么必须以管理员身份运行,要么禁用 UAC 才能正常工作,但两者都不推荐。
相反,您可以写入“应用程序数据”区域中的文件夹。
如果您使用 .config 文件来配置日志,则可以使用类似
或 的
内容,具体取决于您是否希望日志文件特定于用户。
(显然,您可以将 CompanyName 和 ProductName 替换为您自己的详细信息)。
这应该适用于 Xp/Vista/W7。
You should not be trying to write directly to the root folder. Under windows 7, you will either have to run as administrator or disable UAC for that to work and neither are recommended.
Instead you can write to a folder in the 'application data' area
If you are using a .config file to configure log, you can use something like
or
depending on whether you want the log files to be specific to a user or not.
(Obviously you replace CompanyName and ProductName with your own details).
This should work on Xp/Vista/W7.
在我看来,你有 3 个选择:
就像提到的,始终以管理员身份运行您的应用程序,尽管这不是一个出色的解决方案
使用本地路径执行应用程序的本地路径来存储您的日志 - 我总是更喜欢这种方法,因为我总是知道我的日志在哪里( AppDomain.CurrentDomain.BaseDirectory 将帮助您)
使用“我的文档”或一些类似的特殊文件夹 - 快速谷歌给我们:特殊文件夹
我希望这会有所帮助。
You have 3 options in my eyes:
like mentioned always run your app as admin altough thats not a brilliant solution
Use the local path of the executing app to store your log - I always prefer this method as I always know where my logs are ( AppDomain.CurrentDomain.BaseDirectory will help you)
Use "My Documents" or some similar special folders - a quick google gives us: special folders
I hope this helps.