Reputation: 339
I have created a windows service program & install the service: following configuration used for log4net:
<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>
When I start service manual it is logging properly for first time but it is not logging when it runs after defined interval. I have used timer in code to run process in every 24 hours. But it is not creating log on next period.
System.Threading.Timer _timer = new System.Threading.Timer(new System.Threading.TimerCallback(TimerTick),
null,
0,
24 * 3600000
);
Timer will be called in every 24 hours:
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);
}
}
Upvotes: 0
Views: 1177
Reputation: 6279
This has nothing to do with log4net, since you're not even using it (WriteEntry
is an EventLog
method, not an ILog
method). To use log4net, you need to confgure, get the logger from LogManager
, then use the logging methods. Sample:
log4net.Config.XmlConfigurator.Configure();
var logger = LogManager.GetLogger("root");
log.Info("Information message");
However, the issue you're having is with Timer
. I would recommend accomplishing what this answer has, since it will start the next timeout after the current callback is finished.
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
}
Otherwise, you could fix your timer so it calls the callback after a certain timeout by doing:
_timer = new Timer(new TimerCallback(timerTick),
null,
0, // Set a time here for an initial timeout
_timeoutInMilliseconds // It will call timerTick every 10 seconds
);
Upvotes: 1