log4net写入文件.如何打开权限 [英] log4net writing to file. How to open up permissions

查看:308
本文介绍了log4net写入文件.如何打开权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XP机器上,我很高兴将log4net与WPF程序一起使用,并且很高兴地使用fileAppender FileAppender将日志消息写入c:\ log.txt.一切都好.但是,它在Windows 7计算机上不起作用.没有错误或任何东西,只是没有创建文件,更不用说登录了.一项小小的研究表明,这是Windows 7的文件权限问题(UAC),实际上,如果我以管理员身份运行可执行文件,它就可以工作.如果我只是单击它(即使我以管理员身份登录)也不起作用,并且当我从Visual Studio启动时它也不起作用.

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.

问题: 1.有人可以给我指出一个示例,在该示例中,我请求允许写入一个且只有一个文件(C:\ log.txt).我已经看到了一些示例,这些示例将app.config配置为要求整个程序以管理员权限运行.这似乎有些矫kill过正,但我​​想它会起作用. 2.有没有更好的方法将信息发送到日志文件?毕竟,用户计算机上可能不存在C:.我想我想起了Windows 7中用户分区"的想法,但是无论如何我都必须在XP和Vista上工作.

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.

一吨, 戴夫

推荐答案

您不应尝试直接写入根文件夹.在Windows 7下,您要么必须以管理员身份运行,要么要禁用UAC才能正常工作,并且都不推荐这样做.

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

如果您使用.config文件配置日志,则可以使用类似的

If you are using a .config file to configure log, you can use something like

<file value="${ALLUSERSPROFILE}\CompanyName\ProductName\Log.txt" />

<file value="${APPDATA}\CompanyName\ProductName\Log.txt" />

取决于您是否希望日志文件特定于用户.

depending on whether you want the log files to be specific to a user or not.

(显然,您用自己的详细信息替换了CompanyName和ProductName).

(Obviously you replace CompanyName and ProductName with your own details).

这应该适用于Xp/Vista/W7.

This should work on Xp/Vista/W7.

这篇关于log4net写入文件.如何打开权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆