尝试在 C# 中获取 log4net 日志文件路径时出错
我在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种可能是您尚未加载配置。您是否调用过
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.