1
votes

I have a C# solution that contains three executables. I have each of these three executables sharing the same log4net configuration file. At startup of each of the executable, they retrieve a logger (one logger per executable, as per configuration file further below). When one of the executable performs Log.GetLogger(), it creates all the rolling files instead of only the one rolling file that is referred to as appender-ref in the executable's logger configuration.

For instance, when I startup my sending daemon executable, it performs Log.GetLogger("SendingDaemonLogger") which creates 3 files Log/RuleScheduler.txt, Log/NotificationGenerator.txt and Log/NotificationSender.txt instead of only the desired Log/NotificationSender.txt.

Then when I startup another of the executables, for instance the rule scheduler daemon, this other process cannot write in Log/RuleScheduler.txt because it has been created and locked by the sending daemon process.

I am guessing that there may be three different solutions to my problem:

  1. The GetLogger should only create the rolling file appenders that are referenced in the config

  2. I should have one config file per executable, this way each config file could list only one rolling file appender and starting each of the executable would not create the rolling files of the other daemons. I am however reluctant to do this because some of the configuration (SMTP appender, console appender) is shared between the daemons and I don't want to have duplicate copies to maintain. Unless there is a way to have a config file including another one?

  3. Maybe there is a way to configure the rolling file so that concurrent access across processes is allowed? This solution still isn't perfect in my opinion because any of the daemons should not be creating the rolling files of some other daemons.

Thanks in advance for your help!

I have difficulties for posting the config file properly here (this website interprets as HTML). Please go to the following link for seeing my log4net configuration file: log4Net configuration file

2

2 Answers

1
votes

Better late than never...

Use the following filename definition for an file appender. The %appdomain will be replaced on startup with the appdomains friendly name. Now you can use the exact same configuration file for multiple apps and still have separate logfiles. No need to configure multiple appenders...

<file  type="log4net.Util.PatternString" value="C:\logs\%appdomain.log" />

see PatternString documentation for available patterns.

0
votes

I think that all 3 files are created for the reason given in this answer. Though you could write your own appender that does not show this behavior it is not advisable. This leaves you with either having 3 separate config files or configuring minimal locking:

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

I have no experience with the latter so I cannot tell you how good this works. I would probably go for the three separate config files.