如何使用 Windows 中的 log4net xml 配置器使用文件夹位置指定日志文件路径?
我在我的 app.config 中添加了
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="%programdata%/log-file.txt"/>
但它不起作用。有什么想法吗?
In my app.config I put
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="%programdata%/log-file.txt"/>
but it didn't work. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
用于扩展环境变量的 log4net 语法是“${Variable}”,例如
The log4net syntax for expanding environment variables is "${Variable}" e.g.
在这里复活一个旧线程,但我遇到了同样的问题并认为我会分享。
${PROGRAMDATA},正如在另一个答案的评论线程中讨论的那样,对我不起作用(与OP相同)。但是,我在某处看到了有关区分大小写的评论。尝试了 ${ProgramData} ,果然,它工作得很好。
Resurrecting an old thread here, but I encountered the same issue and thought I'd share.
${PROGRAMDATA}, as discussed in the comment thread of the other answer, didn't work for me (same as for OP). However, I saw a comment somewhere about it being case sensitive. Tried ${ProgramData} and sure enough, it worked fine.
使用 ${APPDATA} 而不是 ${LOCALAPPDATA}
Use ${APPDATA} instead of ${LOCALAPPDATA}
我们发现,当将 Log4Net 与 DotNetCore 和 Microsoft.Extensions.Logging.Log4NetProvider 扩展方法一起使用时,环境变量不会传递。这是因为 log4net 配置器是通过将 log4net 配置文件解析为 XML 进行配置的,然后将其传递给 log4net 配置器,从而绕过环境变量替换。但是,您可以通过执行以下操作来覆盖属性:
We have found that when using Log4Net with DotNetCore and the Microsoft.Extensions.Logging.Log4NetProvider extension method environment variables are not passed through. This is because the log4net configurator is configured by parsing the log4net config file into XML before passing it to the log4net configurator which bypasses the environment variable substitution. However, you can override properties by doing the following: