log4net动态配置

发布于 2024-11-19 02:26:33 字数 1172 浏览 2 评论 0原文

我有一个 VB 控制台应用程序,我正在尝试动态命名 FileAppender 的 log4net 输出文件。

正在创建日志文件并且它具有正确的内容,问题是正在使用名称“%property{LogFilePath}”创建该文件。换句话说,它根本不进行字符串替换。

在 app.config 中:

<log4net>
    <appender name="myAppender" type="log4net.Appender.FileAppender">
        <file value="%property{LogFilePath}" />
        <appendToFile value="false" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %-5level - %message%newline" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="myAppender" />
    </root>
</log4net>

在我的 VB 文件中:

Private _logger As ILog

Private Sub InitializeLogger()
    log4net.GlobalContext.Properties("LogFilePath") = "C:\Logs\myLog.log"
    XmlConfigurator.Configure()
    _logger = LogManager.GetLogger("myAppender")
End Sub

回顾一下,正在创建日志文件并且它确实具有预期的内容,唯一的问题是日志文件名保留为“%property{LogFilePath}”而不是被替换通过“C:\Logs\myLog.log”

任何帮助将不胜感激:)

I have a VB console app and I am trying to dynamically name the log4net output file of my FileAppender.

The log file is being created and it has the proper contents, the problem is that the file is being created with the name "%property{LogFilePath}". In other words, it is not doing the string replace at all.

In app.config:

<log4net>
    <appender name="myAppender" type="log4net.Appender.FileAppender">
        <file value="%property{LogFilePath}" />
        <appendToFile value="false" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %-5level - %message%newline" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="myAppender" />
    </root>
</log4net>

In my VB File:

Private _logger As ILog

Private Sub InitializeLogger()
    log4net.GlobalContext.Properties("LogFilePath") = "C:\Logs\myLog.log"
    XmlConfigurator.Configure()
    _logger = LogManager.GetLogger("myAppender")
End Sub

To recap, the log file IS being created and it does have the expected content, the only problem is that the log file name is remaining as "%property{LogFilePath}" instead of being replaced by "C:\Logs\myLog.log"

Any help would be greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2024-11-26 02:26:33

您的文件属性中是否缺少类型?

<file type="log4net.Util.PatternString" value="%property{LogFilePath}" />

有关更多详细信息,请参阅此:
http://logging.apache.org/log4net/release/sdk /log4net.Util.PatternString.html

Could you be missing the type in your file attribute?

<file type="log4net.Util.PatternString" value="%property{LogFilePath}" />

See this for more detail:
http://logging.apache.org/log4net/release/sdk/log4net.Util.PatternString.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文