0

我目前正在开发一个使用 log4net 的 Windows 服务。该服务包含一个计时器,它以指定的时间间隔处理作业。Log4net 工作正常,服务本身也工作正常,但是,每次计时器经过时,log4net 都会写入一个空白的“log.Info”条目。

空白条目如下所示:

2012-05-15 14:58:34,141 [9] 信息 ServiceHelper.JobOperations [(null)] -

请在下面找到 Windows 服务的基本结构。有没有其他人遇到过这个奇怪的问题?

服务控制器类

public partial class JobProcessor : ServiceBase
{
    private JobOperations _operations;

    public JobProcessor()
    {
        XmlConfigurator.Configure(); //init log4net here

        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        _operations = new JobOperations();
        _operations.Start();
    }

    protected override void OnStop()
    {
        _operations.Stop();
    }
}

服务主类:

public class JobOperations
{
    // Logging...
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    protected Timer Tmr;

    public JobOperations()
    {
        var interval = 3000;

        Tmr = new Timer(interval);

        Tmr.Elapsed += tmr_Elapsed;
    }

    public void Start()
    {
        Tmr.Start();
        log.Info("Job Processor: STARTED.");
    }

    public void Stop()
    {
        Tmr.Stop();
        log.Info("Job Processor: STOPPED.");
    }

    //
    // Main operation for Windows Service
    public static void CheckJobQueue()
    {
        // Do stuff in here...
    }

    //
    // Timer Elapsed Method Call..
    protected void tmr_Elapsed(object sender, ElapsedEventArgs e)
    {
        //
        // Halt timer...
        Tmr.Stop();

        CheckJobQueue();

        //
        // Start timer...
        Tmr.Start();
    }
}

* 更新 * : 感谢下面评论的人,首先,我在下面附加了我的 log4net 配置。其次,除了当前正在记录的空白消息之外,还记录了实际消息。

log4net 配置文件

<?xml version="1.0"?>
<log4net>
  <root>
    <level value="ALL"/>
    <appender-ref ref="SmtpAppender"/>
    <appender-ref ref="RollingLogFileAppender"/>
  </root>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="G:\pathToLogFiles\"/>
    <datePattern value="'Application_'yyyyMMdd'.log'"/>
    <appendToFile value="true"/>
    <maximumFileSize value="1024KB"/>
    <maxSizeRollBackups value="5"/>
    <rollingStyle value="Date"/>
    <staticLogFileName value="false"/>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
    </layout>
  </appender><!--LocalFile logging (Daily)-->
  <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="something@someone.com"/>
    <from value="someone@something.com"/>
    <subject value="*** SERVER ERROR ***"/>
    <smtpHost value="smtp.gmail.com"/>
    <authentication value="Basic"/>
    <port value="587"/>
    <username value="something@someone.com"/>
    <password value="mysupersecretusername"/>
    <EnableSsl value="true"/>
    <bufferSize value="1"/>
    <lossy value="true"/>
    <evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="ERROR"/>
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline"/>
    </layout>
  </appender><!--SMTP Logging (Critical Errors)-->
</log4net>
4

0 回答 0