20

I am using log4net and I was to save the log file in the AppData file for win XP/Vista etc.

This is my app.config file so far, and I have specified the name softphone.log. Hoewver, I am not sure how to specify the complete path to the file as each user will have a different path depending on their username.

<log4net>
    <logger name="default">
      <level value="DEBUG"/>
    </logger>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="softphone.log"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackup value="10"/>
      <maximumFileSize value="1MB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
      </layout>
    </appender>
  </log4net>

In my source code I can get the path by doing the following:

System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

However, I can't use the above in my app.config and if I hard coded the path on my system the path would be:

C:\Documents and Settings\John\Application Data

However, this would be different for each client. So is there a way to do this for the app.config file?

Many thanks for any suggestions,

ant2009
  • 27,094
  • 154
  • 411
  • 609
  • possible duplicate of [How to specify common application data folder for log4net?](http://stackoverflow.com/questions/468989/how-to-specify-common-application-data-folder-for-log4net) – Anthony Mastrean Jun 16 '11 at 03:47

2 Answers2

20

I don't believe that you can do what you want, there is a method for custom parsing areas of the app.config file so that you could add your own token that you could replace with the correct value, but I don't see how that would work inside the log4net section.

However, everything that is set up for log4net inside the config can also be set in code. I think you're best option would be to set the property for the appender in code just after application start.


Ahh, never mind a quick search has revealed my ignorance. From here and here it appears that something similar to this:

<file value="${APPDATA}\log-file.txt" />

Will do what you want. I haven't tested this myself, so I'll leave my first answer up too - but I'd be interested to know if you have any luck with it.

Martin Harris
  • 28,277
  • 7
  • 90
  • 101
0

User %appdata%. So your code will look like -

<file value="%appdata%/softphone.log"/>
Vivek Raj
  • 85
  • 7