我试图通过log4net获取带有日志的文件名,但无法获取Appenders。这是我的配置:
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="name.log"/>
<appendToFile value="false"/>
<maximumFileSize value="10MB"/>
<maxSizeRollBackups value="0"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline"/>
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR"/>
<foreColor value="Red, HighIntensity"/>
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="ColoredConsoleAppender"/>
</root>
</log4net>
这是用于获取附加程序计数的代码:
((Hierarchy)LogManager.GetRepository()).GetAppenders().Length.ToString();
但它给了我0。日志记录非常有效,所以config适用于日志记录。你知道如何在没有appenders的情况下从配置中获取appenders或文件名"name.log"
吗?非常感谢。
是否可能在调用Configure之前获取appenders?
使用您提供的配置:
int count;
count = LogManager.GetRepository().GetAppenders().Length; // 0
XmlConfigurator.Configure();
count = LogManager.GetRepository().GetAppenders().Length; // 2