Windows服务中的log4net在计时器上创建空白条目已过

时间:2012-05-15 05:28:28

标签: c# windows-services log4net

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

空白条目如下所示:

2012-05-15 14:58:34,141 [9] INFO 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>

0 个答案:

没有答案