C# 当windows服务自动运行时,log4net不工作
我创建了一个windows服务程序并安装该服务:用于log4net的以下配置:C# 当windows服务自动运行时,log4net不工作,c#,windows-services,log4net,C#,Windows Services,Log4net,我创建了一个windows服务程序并安装该服务:用于log4net的以下配置: <log4net> <root> <level value="All" /> <appender-ref ref="RollingLogFileAppender" /> </root> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppend
<log4net>
<root>
<level value="All" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="D:\\Logs\\Menca.DataImportService\\%property{LogName}\\%property{LogName}.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="2000" />
<maximumFileSize value="500KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
计时器将每24小时调用一次:
private void TimerTick(object sender)
{
try
{
// Thread.Sleep(15000);
rCount = 0;
iCount = 0;
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
_eventLog.WriteEntry(string.Concat("Scheduled Service Call Start Hour: ", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
Helper.StartDataDump();
_eventLog.WriteEntry(string.Concat("Scheduled Service Call End Hour: ", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
_eventLog.WriteEntry(string.Concat("Scheduled service call was processed successfully! : ", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
}
catch (Exception ex)
{
string errMsg = ex.InnerException == null ? ex.InnerException.Message : ex.Message;
_eventLog.WriteEntry("Error occured in scheduled service call Exception: " + errMsg);
}
}
这与log4net无关,因为您甚至没有使用它(
WriteEntry
是一种EventLog
方法,而不是ILog
方法)。要使用log4net,您需要配置,从LogManager
获取记录器,然后使用日志记录方法。样本:
log4net.Config.XmlConfigurator.Configure();
var logger = LogManager.GetLogger("root");
log.Info("Information message");
但是,您遇到的问题是计时器
。我建议完成已完成的操作,因为它将在当前回调完成后启动下一个超时
private Timer _timer;
private ILog _logger = LogManager.GetLogger("root"); // Get the log4net logger
private const int _timeoutInMilliseconds = 10000; // 10 seconds
public void Start()
{
_timer = new Timer(new TimerCallback(timerTick),
null,
_timeoutInMilliseconds, // Timeout to wait until the callback is called
Timeout.Infinite // Makes sure it's only called once
);
}
private void timerTick(object sender)
{
_logger.Info("Sample");
_timer.Change(_timeoutInMilliseconds, Timeout.Infinite); // Wait 10 seconds again
}
否则,您可以通过执行以下操作修复计时器,使其在特定超时后调用回调:
_timer = new Timer(new TimerCallback(timerTick),
null,
0, // Set a time here for an initial timeout
_timeoutInMilliseconds // It will call timerTick every 10 seconds
);
您的TimerTick方法似乎没有使用Log4net!我在Helper.StartDataDump()中使用了它;我没有在上面提到,无论如何,最后我发现问题是log4net在服务启动时只初始化了一次&然后它将在写入日志时使用相同的配置,但在相同的配置路径上。
_timer = new Timer(new TimerCallback(timerTick),
null,
0, // Set a time here for an initial timeout
_timeoutInMilliseconds // It will call timerTick every 10 seconds
);