log4net writing to file. How to open up permissions

╄→гoц情女王★ 提交于 2019-12-23 02:24:24

问题


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


回答1:


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

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

or

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

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.




回答2:


You have 3 options in my eyes:

  1. like mentioned always run your app as admin altough thats not a brilliant solution

  2. 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)

  3. Use "My Documents" or some similar special folders - a quick google gives us: special folders

I hope this helps.



来源:https://stackoverflow.com/questions/7591762/log4net-writing-to-file-how-to-open-up-permissions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!