I have written a C# app that runs constantly in a loop and several threads write to a log4net file.
The issue is that the longer the app is running, the more time it takes to complete a loop. I have run a ANTS Performance profiler, and noticed that most of this CPU time is spent logging with log4.net.
The more verbose the logs the more CPU it uses and after 30 minutes it's using 100% of the CPU. If I disable logging the time taken for a loop remains constant over time. I had a look at Windows Performance Monitor and the Physical Disk is most of the time IDLE.
I have tried to keep my logging to a minimum, but even with relatively low amounts of logging im still experiencing the issue.
Here is a sample of my Log4net.xml configuration file:
<log4net>
<root>
<!-- Levels: OFF, DEBUG, INFO, WARN, ERROR, FATAL-->
<level value="INFO" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<!--Logs to a file-->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\\logs\\log-file.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+ExclusiveLock" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="20" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %L %M Schdl:[%property{scheduleContext}] Job:[%property{jobContext}] - %message%newline" />
</layout>
</appender>
</log4net>
I am using the same logger from every object that logs.
log4net.ILog logger;
log4net.Config.BasicConfigurator.Configure();
logger = log4net.LogManager.GetLogger(typeof(myProject));
Why would it use more and more CPU the longer it runs?
Any advise on how to improve this would be appreciated.
Serilog has the concept of Sinks and log4net has appenders. They both cover the same need, just with different naming. Sinks and appenders are available for a lot of different destinations like files, databases, and remote services. As an example, elmah.io provides both a sink for Serilog and an appender for log4net.
There is some small difference between NLog and log4net. NLog is easier to configure, and supports a much cleaner code-based configuration than log4net. I think the defaults in NLog are also more sensible than in log4net.
Log4j is a very popular logging framework for Java developers, used by most Java applications. It has been ported to other programming languages including log4perl, log4php, log4net, and log4r.
Log4net is a synchronous engine.
Are you configuring log4net in every object that logs? It looks like that from your code. Configuration should be done once per process, e.g. at startup and then your objects should only have to acquire the logger.
For each class that needs to log I usually have the following pattern:
class SomeClass
{
private static readonly ILog log = LogManager.GetLogger(typeof(SomeClass));
...
}
Using this pattern you will automatically get loggers that are hierarchical according to the namespace and name of the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With