问题描述
我使用log4net的,我是到日志文件保存为赢XP / Vista的等方面的应用程序数据文件。
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。 Hoewver,我不知道如何指定该文件为每个用户都会有根据他们的用户名不同的路径的完整路径。
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?
任何建议非常感谢,
推荐答案
我不相信你可以做你想做的,有一个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的配置里面的一切也可以在代码中设置。我觉得你最好的办法是设置刚过应用程序启动的追加程序代码的属性。
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文件中的应用程序数据文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!