0

过去一天我一直在尝试解决 log4net 问题。要求是我有多个记录器根据正在处理的特定服务器端事件创建唯一的日志文件。问题是当我创建多个附加程序时,我的日志文件的输出始终为 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&#xD;&#xA;" />
        <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&#xD;&#xA;" />
        <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&#xD;&#xA;" />
        <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&#xD;&#xA;" />
        <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&#xD;&#xA;" />
        <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>

下面是指向多个附加程序的 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)

当我创建附加程序时,我认为在我的 app.config 文件中做错了什么。当我创建一个根附加程序并在根级别引用它时它可以工作,但是当我创建记录器并以这种方式引用附加程序时,它似乎失败了。我没有想法 - 建议?谢谢。

4

1 回答 1

0

尝试 ALL 的根级别设置怎么样?

可能是一个红鲱鱼,但我注意到您的代码使用 log.INFO 并且您的配置指定 DEBUG?

于 2012-05-17T17:23:58.537 回答