1

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

Dave
  • 8,095
  • 14
  • 56
  • 99

2 Answers2

5

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.

sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • Thank you very much sgmoore. Can I ask one follow-up? I'm sharing my .config file amongst several projects. I'd like the file to be put under the solution, project, or assembly name. I've tried: but that does not work. I'm sure there's some macro like $Solution or $Project to make this work. What I have just saves it under ...\$AssemblyName$ – Dave Oct 01 '11 at 17:50
  • Also, for the millions of viewers, :), who will look at this later, XP saves the file under Documents and Settings\CompanyName... but Windows 7 puts it under: C:\Users\All Users\MyCompany\$AssemblyName$ This confused me briefly. – Dave Oct 01 '11 at 17:50
  • The closest would probably be %appdomain which will usually be the name of the exe. See http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html for the full list. – sgmoore Oct 01 '11 at 19:10
0

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.

hyp
  • 1,370
  • 1
  • 9
  • 21