简体   繁体   中英

Where are the log4net Log files?

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:

${LocalAppData}/MyProgram/Logs/Filename

Or just:

${AppData}/MyProgram/Logs/Filename

As for which of these two to pick - I'm not sure what "roaming" is, so I don't know why you'd prefer AppData or LocalAppData . Probably worth a different question :)

It's a permissions issue. In Windows 7 standard users cannot write to the Program Files directory. So if you are not running your application as administrator Log4Net cannot create this subdirectory. You will have to either grant write permissions to anyone to the Logs/LogFileName directory while installing the application or put the log file in the corresponding c:\\users\\username\\Application Data\\AppName folder.

On Windows7, you are supposed to write to the C:\\Users\\<CurrentUser> folder. You may create the installlog folder here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM