Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net performance

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.

like image 596
Benjamin Ortuzar Avatar asked Mar 09 '10 16:03

Benjamin Ortuzar


People also ask

Which is better log4net or Serilog?

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.

Which is better NLog or 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.

Is log4net using Log4j?

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.

Is log4net asynchronous?

Log4net is a synchronous engine.


1 Answers

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.

like image 147
Peter Lillevold Avatar answered Sep 24 '22 02:09

Peter Lillevold