Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how to specify the appData file path in the app.config file

Tags:

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,

like image 727
ant2009 Avatar asked Jun 08 '09 09:06

ant2009


1 Answers

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.

like image 72
Martin Harris Avatar answered Sep 21 '22 11:09

Martin Harris