过去一天,我一直在尝试解决log4net问题。要求是我有多个记录器根据正在处理的特定服务器端事件创建唯一的日志文件。问题是,当我创建多个appender时,我的日志文件的输出始终为NULL。
以下是我的app.config文件中的相关log4net块。
<log4net>
<root>
<level value="DEBUG" />
</root>
<logger name="AffectivExcitementShortTermScore">
<level value="DEBUG" />
<appender-ref ref="AffectivExcitementShortTermScore" />
</logger>
<logger name="AffectivExcitementLongTermScore">
<level value="DEBUG" />
<appender-ref ref="AffectivExcitementLongTermScore" />
</logger>
<logger name="AffectivEngagementBoredomScore">
<level value="DEBUG" />
<appender-ref ref="AffectivEngagementBoredomScore" />
</logger>
<logger name="AffectivFrustrationScore">
<level value="DEBUG" />
<appender-ref ref="AffectivFrustrationScore" />
</logger>
<logger name="AffectivMeditationScore">
<level value="DEBUG" />
<appender-ref ref="AffectivMeditationScore" />
</logger>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="C:\temp\emotiv_log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
<appender name="AffectivExcitementShortTermScore" type="log4net.Appender.FileAppender">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="C:\\logs\\AffectivExcitementShortTermScore.csv">
</file>
<appendToFile value="true" />
<layout type="DataMind.CsvPatternLayout, DataMind">
<header value="DateTime,SystemUpTime,Value,EventTimer
" />
<conversionPattern value="%date{M/d/yyyy H:mm:ss.fff}%newfield%property{SystemUpTime}%newfield%property{Value}%newfield%property{EventTimer}%endrow" />
</layout>
<filter type="log4net.Filter.PropertyFilter">
<Key value="Version" />
<StringToMatch value="1" />
</filter>
</appender>
<appender name="AffectivExcitementLongTermScore" type="log4net.Appender.FileAppender">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="C:\\logs\\AffectivExcitementLongTermScore.csv">
</file>
<appendToFile value="true" />
<layout type="DataMind.CsvPatternLayout, DataMind">
<header value="DateTime,SystemUpTime,Value,EventTimer
" />
<conversionPattern value="%date{M/d/yyyy H:mm:ss.fff}%newfield%property{SystemUpTime}%newfield%property{Value}%newfield%property{EventTimer}%endrow" />
</layout>
<filter type="log4net.Filter.PropertyFilter">
<Key value="Version" />
<StringToMatch value="1" />
</filter>
</appender>
<appender name="AffectivEngagementBoredomScore" type="log4net.Appender.FileAppender">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="C:\\logs\\AffectivEngagementBoredomScore.csv">
</file>
<appendToFile value="true" />
<layout type="DataMind.CsvPatternLayout, DataMind">
<header value="DateTime,SystemUpTime,Value,EventTimer
" />
<conversionPattern value="%date{M/d/yyyy H:mm:ss.fff}%newfield%property{SystemUpTime}%newfield%property{Value}%newfield%property{EventTimer}%endrow" />
</layout>
<filter type="log4net.Filter.PropertyFilter">
<Key value="Version" />
<StringToMatch value="1" />
</filter>
</appender>
<appender name="AffectivFrustrationScore" type="log4net.Appender.FileAppender">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="C:\\logs\\AffectivFrustrationScore.csv">
</file>
<appendToFile value="true" />
<layout type="DataMind.CsvPatternLayout, DataMind">
<header value="DateTime,SystemUpTime,Value,EventTimer
" />
<conversionPattern value="%date{M/d/yyyy H:mm:ss.fff}%newfield%property{SystemUpTime}%newfield%property{Value}%newfield%property{EventTimer}%endrow" />
</layout>
<filter type="log4net.Filter.PropertyFilter">
<Key value="Version" />
<StringToMatch value="1" />
</filter>
</appender>
<appender name="AffectivMeditationScore" type="log4net.Appender.FileAppender">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="C:\\logs\\AffectivMeditationScore.csv">
</file>
<appendToFile value="true" />
<layout type="DataMind.CsvPatternLayout, DataMind">
<header value="DateTime,SystemUpTime,Value,EventTimer
" />
<conversionPattern value="%date{M/d/yyyy H:mm:ss.fff}%newfield%property{SystemUpTime}%newfield%property{Value}%newfield%property{EventTimer}%endrow" />
</layout>
<filter type="log4net.Filter.PropertyFilter">
<Key value="Version" />
<StringToMatch value="1" />
</filter>
</appender>
</log4net>
以下是指向多个appender的GetLogger方法:
public class SomeClass
{
protected static readonly ILog log = LogManager.GetLogger(typeof(Namespace.SomeClass));
private static readonly ILog log0 = LogManager.GetLogger("AffectivExcitementShortTermScore");
private static readonly ILog log1 = LogManager.GetLogger("AffectivExcitementLongTermScore");
private static readonly ILog log2 = LogManager.GetLogger("AffectivEngagementBoredomScore");
private static readonly ILog log3 = LogManager.GetLogger("AffectivFrustrationScore");
private static readonly ILog log4 = LogManager.GetLogger("AffectivMeditationScore");
最后,我按如下方式记录我的事件:
log0.Info(new Log
{
SystemUpTime = FormatUpTime(_states.TimeFromStart),
Value = _states.AffectivExcitementShortTermScore.ToString(),
EventTimer = _eventTimer
});
// log longterm exictement
log1.Info(new Log
{
SystemUpTime = FormatUpTime(_states.TimeFromStart),
Value = _states.AffectivExcitementLongTermScore.ToString(),
EventTimer = _eventTimer
});
我的结果(如下)始终显示日志未写入文件:
DateTime SystemUpTime Value EventTimer
37:07.6 (null) (null) (null)
37:07.6 (null) (null) (null)
37:07.6 (null) (null) (null)
37:07.6 (null) (null) (null)
37:07.7 (null) (null) (null)
37:07.7 (null) (null) (null)
当我创建我的appender时,我认为我在app.config文件中做错了。它在我创建一个根appender并在根级别引用它时工作,但是当我创建记录器并以这种方式引用appender时,它似乎失败了。我没有想法 - 建议?感谢。
答案 0 :(得分:0)
尝试ALL的根级别设置怎么样?
可能是红鲱鱼,但我注意到你的代码使用了log.INFO而你的配置指定了DEBUG?