C#如何在app.config文件中指定appData文件路径 [英] C# how to specify the appData file path in the app.config file

查看:50
本文介绍了C#如何在app.config文件中指定appData文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 log4net,我将日志文件保存在 Win XP/Vista 等的 AppData 文件中.

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

到目前为止,这是我的 app.config 文件,我已经指定了名称 softphone.log.但是,我不确定如何指定文件的完整路径,因为每个用户的路径不同,具体取决于他们的用户名.

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)

但是,我不能在我的 app.config 中使用上述内容,如果我在系统上硬编码路径,路径将是:

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

但是,这对于每个客户来说都是不同的.那么有没有办法对 app.config 文件执行此操作?

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,

推荐答案

我不相信你可以为所欲为,有一种方法可以自定义解析 app.config 文件的区域,这样你就可以添加你的自己的令牌,您可以用正确的值替换它,但我看不出它在 log4net 部分中是如何工作的.

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.

但是,在配置中为 log4net 设置的所有内容也可以在代码中设置.我认为您最好的选择是在应用程序启动后立即在代码中为 appender 设置属性.

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.

这篇关于C#如何在app.config文件中指定appData文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆