How to obtain all log4net loggers currently running in all processes - log4net

Each process in our system uses log4net. Many of those processes are stared up as services. Some of those running applications are very verbose spewing debug entries and we'd like the ability to limit their output. All loggers use the same .config file to set their current logging level. I'm working on an app that will gain access to all loggers that set the source using
log4net.GlobalContext.Properties["source"] = source;
What I'd like to do is get a list of all loggers currently registered, obtain their source name and then change the .config file for each of those sources to individually configure the output level for each of them.
The problem I'm having is finding the list of all those loggers so I may add an entry in the .config file. Is there a way to do obtain that list of loggers across all processes from a single application?
I've tried using GetRepository().GetCurrentLoggers(), but that only returns the logger for the currently calling application.

There is no standard way of getting loggers which are in an other process. You have to make something your self.

Try this
using log4net.Repository.Hierarchy;
var appenders = ((Hierarchy)LogManager.GetRepository())
.Root.Appenders;

Related

Where does Log4Net store it's logs?

I have a WinForms application and I'm currently evaluating Log4Net.
When I close the application and delete the log file, I expect to lose the logs. What happens is I launch the application and the logs of the last session get written in the file.
Log4net does not store the logs in a specific place. You can configure log4net where to store the logs. The basic principle is that you configure log appenders in configuration. This way you are flexible in where you are logging. There are many possible appenders, for example:
file
sql
eventlog
debug
blob storage
...
for a overview of 'out of the box' appenders: https://logging.apache.org/log4net/log4net-1.2.13/release/sdk/log4net.Appender.html
Each appender has different configuration options. You can have multiple appenders to log to different 'endpoints'.

Logging of multiple appenders to same logger

I want to implement same logfile for different appenders ..Is it possible to do so?or should i have to use different log files?
To be on the safe side it is probably better not to do this as it could lead to synchronization issues, deadlock or corrupt log files.
However, logback (log4j's successor) allows this, in prudent mode.
Another alternative, is to have your multiple appenders log to a single SocketAppender and have the receiving socket log to a file.
Don't know log4j, but log4net has ForwardingAppender and BufferingForwardingAppender, and I guess it's probably similar.
So presumably you could have multiple ForwardingAppenders that all forward to the same FileAppender.

How to configure log4net for fallback

This is my situation. I have successfully implemented logging to remote syslog using log4net. However, as far as I could test, if syslog IP is not valid, all messages will not log anywhere and no exception is raised. It just does nothing.
Hence, it would be nice to have some sort of fallback. Let's say if writing to syslog fails, write to file or to database.
Is that even possible with log4net? Or would I have to configure it to log to two locations at the same time?
I don't think you can do this by configuration. This issue is open in the log4net feature backlog.
If your application can eat the logging overhead, the easiest solution would be to log to an alternative appender by default.
Alternatively you could try to wrap the appender you're using in a custom appender, and implement the fallback scenario if the syslog appender throws an exception. If it doesn't swallow them silently.
From log4net FAQ:
You can implement the log4net.Appender.IAppender interface to create you own customized appender. We recommend that you extend the log4net.Appender.AppenderSkeleton class rather than starting from scratch. You should implement your custom code in a assembly separate from the log4net assembly.
To get started it is worth looking at the source of the log4net.Appender.TraceAppender as an example of the minimum amount of code required to get an appender working.
Third option would be to look into the source code of your appender and see if you can fork it and do the necessary customizations there.

Log4Net GetLogger creates rolling files even for the unreferenced files

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:
The GetLogger should only create the rolling file appenders that are referenced in the config
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?
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
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.
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.

Single Log4j between Tomcat6 webapps

Is there a correct way to use Log4j logger with Common ClassLoader, not with Web-App based ClassLoader, i.e. use only one log4j.jar for all deployed web-apps?
When i'm drop a log4j.jar in commons lib directory, log4j get a first loj4j.properties for any application and use only it. All other loj4j.properties (of other applications) are ignored. Is there way to say log4j to return separate log factory for every application with their properties?
Not in the way I think you mean, because each webapp's loggers should be completely independent of loggers in other webapps. Because logging repositories are singletons, this can only be done if they are handled by separate Classloaders, i.e. the webapp class loaders.
Also note that in general (though it may not be the case for you), different webapps have different lifecycles, which means that Tomcat may have to support webapp A using one version of log4j at the same time as webapp B, which uses a different version of log4j.

Resources