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

2

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,

Drillmaster answered 8/6, 2009 at 9:51 Comment(1)
possible duplicate of How to specify common application data folder for log4net?Telega
X
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.

Xeroderma answered 8/6, 2009 at 10:15 Comment(4)
Glad you edited ;) There's always a way of getting to the environment variables somehow.Funnel
I will give it a try and report back with my results. Thanks.Drillmaster
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.Headreach
Both links are dead :( I tried figuring out a new link for both, but no luck.Sigvard
B
0

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

<file value="%appdata%/softphone.log"/>
Briney answered 23/4, 2020 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.