I have a Windows form App which write to log files using log4net. The log file path in the app.config file is defined as:
<file type="log4net.Util.PatternString" value="Logs/LogFileName" />.
I have an msi setup project, which installs the above app. I install the App on the development machine, run the installed App, and as expected, it writes Log files under the directory, InstallDirectory/Logs/. The dev machine has a Win XP SP3 with all read write permissions.
But when I install the same on a testing machine, which has Windows 7 and I am not sure of the permissions, there is no “Logs” directory and no log files.
In both cases, InstallDirectory is C:\Program Files\Dir1\Dir2.
While I try to figure out where the files are, on the test machine by adding:
log4net.Appender.FileAppender rootAppender = (log4net.Appender.FileAppender)((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Appenders[0];
string filename = rootAppender.File;
MessageBox.Show(filename);
to the code, can someone please shed some light.
- Why could the files be missing?
- Does Win 7 has any different ways to deal with new files into the Windows installed drive?
As others have said, don’t run as admin to work around this issue. Write to a folder/file that you have access to.
You can expand environment variables in your configuration to get special folders:
https://web.archive.org/web/20121226213809/http://www.l4ndash.com/Log4NetMailArchive/tabid/70/forumid/1/postid/16299/view/topic/Default.aspx
(or, as recommended in the deleted post on this question by Jon Skeet, an article that is a bit more generic: Where Should I Write Program Data Instead of Program Files?)
Try:
Or just:
As for which of these two to pick – I’m not sure what “roaming” is, so I don’t know why you’d prefer
AppDataorLocalAppData. Probably worth a different question 🙂