tags:

views:

2462

answers:

1

Hello,

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,

+3  A: 

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
Glad you edited ;) There's always a way of getting to the environment variables somehow.
OJ
I will give it a try and report back with my results. Thanks.
robUK
Just to say that this works okay for me, I've only tested it under XP at the moment, but I assume it works on Vista also.
Matt Warren