尝试在 C# 中获取 log4net 日志文件路径时出错

发布于 2024-12-17 15:06:23 字数 1496 浏览 0 评论 0原文

我在尝试在 C# 中检索 log4net 日志文件路径时遇到此错误。 这是我使用的代码。

Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy; // WORKS FINE
Logger logger = hierarchy.Root;                                // WORKS FINE
FileAppender appender = logger.Appenders[0] as FileAppender;   // ERROR!!! - Index was out of range. Must be non-negative and less than the size of the collection. [0] Specified argument was out of the range of valid values.
string file = appender.File;

上面提到的数组中好像没有Appender对象。 有人遇到过这个吗?

App.config 中的我的 log4net 配置部分:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net debug="true">
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
        <param name="File" value="C:\\SampleLog\\log.txt" />
        <param name="AppendToFile" value="true" />
        <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
        </layout>
    </appender>
    <root>
        <priority value="ALL" />
        <appender-ref ref="LogFileAppender" />
    </root>
    <category name="Sample.Form1">
        <priority value="ALL" />
    </category>
</log4net>

I'm coming across this error while trying to retrieve the log4net log file path in C#.
This is the code I used.

Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy; // WORKS FINE
Logger logger = hierarchy.Root;                                // WORKS FINE
FileAppender appender = logger.Appenders[0] as FileAppender;   // ERROR!!! - Index was out of range. Must be non-negative and less than the size of the collection. [0] Specified argument was out of the range of valid values.
string file = appender.File;

It seems that there are no Appender objects in the array mentioned above.
Has anyone come accross this?

My log4net config section from App.config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<log4net debug="true">
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
        <param name="File" value="C:\\SampleLog\\log.txt" />
        <param name="AppendToFile" value="true" />
        <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
        </layout>
    </appender>
    <root>
        <priority value="ALL" />
        <appender-ref ref="LogFileAppender" />
    </root>
    <category name="Sample.Form1">
        <priority value="ALL" />
    </category>
</log4net>

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

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

发布评论

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

评论(1

往日 2024-12-24 15:06:23

一种可能是您尚未加载配置。您是否调用过XmlConfigurator.Configure()

如果是这样,您是否在调用之前执行有问题的代码?也许就是这么简单。

One possibility is that you haven't loaded your configuration. Have you called XmlConfigurator.Configure()?

If so, are you executing the code in question before that call? Maybe it's as simple as that.

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