如何使用 Windows 中的 log4net xml 配置器使用文件夹位置指定日志文件路径?

发布于 2024-09-12 00:50:05 字数 215 浏览 5 评论 0原文

我在我的 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 技术交流群。

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

发布评论

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

评论(4

简美 2024-09-19 00:50:05

用于扩展环境变量的 log4net 语法是“${Variable}”,例如

<file value="${LOCALAPPDATA}\GojiSoft\GojiLog\log.txt" />

The log4net syntax for expanding environment variables is "${Variable}" e.g.

<file value="${LOCALAPPDATA}\GojiSoft\GojiLog\log.txt" />
三岁铭 2024-09-19 00:50:05

在这里复活一个旧线程,但我遇到了同样的问题并认为我会分享。

${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.

挽清梦 2024-09-19 00:50:05

使用 ${APPDATA} 而不是 ${LOCALAPPDATA}

Use ${APPDATA} instead of ${LOCALAPPDATA}

风透绣罗衣 2024-09-19 00:50:05

我们发现,当将 Log4Net 与 DotNetCore 和 Microsoft.Extensions.Logging.Log4NetProvider 扩展方法一起使用时,环境变量不会传递。这是因为 log4net 配置器是通过将 log4net 配置文件解析为 XML 进行配置的,然后将其传递给 log4net 配置器,从而绕过环境变量替换。但是,您可以通过执行以下操作来覆盖属性:

hostBuilder.ConfigureLogging(logBuilder =>
            {
                var logsPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                logBuilder.AddLog4Net(new Log4NetProviderOptions
                    {
                        PropertyOverrides = new List<NodeInfo>
                        {
                            new NodeInfo
                            {
                                XPath = "//appender[@name='rollingFile']//file",
                                Attributes = new Dictionary<string, string>
                                {
                                    {
                                        "value", Path.Combine(logsPath, "Application.log") 
                                    }
                                }
                            }
                        }
                    });
            });
<log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="console" />
    <appender-ref ref="rollingFile" />
  </root>
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level - %message%newline" />
    </layout>
  </appender>
  <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="Application.log" /> <!--This is overidden at run time. Based on the appender name: rollingFile.-->
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
  </appender>
</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:

hostBuilder.ConfigureLogging(logBuilder =>
            {
                var logsPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                logBuilder.AddLog4Net(new Log4NetProviderOptions
                    {
                        PropertyOverrides = new List<NodeInfo>
                        {
                            new NodeInfo
                            {
                                XPath = "//appender[@name='rollingFile']//file",
                                Attributes = new Dictionary<string, string>
                                {
                                    {
                                        "value", Path.Combine(logsPath, "Application.log") 
                                    }
                                }
                            }
                        }
                    });
            });
<log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="console" />
    <appender-ref ref="rollingFile" />
  </root>
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level - %message%newline" />
    </layout>
  </appender>
  <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="Application.log" /> <!--This is overidden at run time. Based on the appender name: rollingFile.-->
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
  </appender>
</log4net>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文