I recently had to face some performance issues on my C# program and find out that log4net
was the origin of those problems. I changed the configuration adding the MinimalLock
and it really helped a lot.
This is how I have now configured my log4net:
var hierarchy = (Hierarchy)log4net.LogManager.GetRepository();
var patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
var roller = new RollingFileAppender();
roller.LockingModel = new FileAppender.MinimalLock();
roller.LockingModel.ActivateOptions();
roller.Encoding = System.Text.Encoding.UTF8;
roller.AppendToFile = true;
roller.File = Path.Combine(Logger.LogPath, "log.txt");
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "100MB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);
hierarchy.Root.Level = Level.Debug;
hierarchy.Configured = true;
The drawback from this is that now my logs contain the very same log lines repeated many times.
I suspect that I misunderstood something from the log4net configuration and now each log line I want to output is sent as much times as processes the application has (even, obviously, the rest of processes created are not logging anything, or at least not the same line at the same time!)
Here an example of log line:
2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,495 [1] DEBUG MetryViewModel - Reading dat: manuel.dat with config: None 2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol
2016-11-18 10:35:34,512 [1] INFO ViewModel - Operating mode: Lol