Logging of multiple appenders to same logger - log4j

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.

Related

How to obtain all log4net loggers currently running in all processes

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;

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'.

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.

log4net :comparing adonetappender and rollingfileappender

I am trying to see which one of these appenders would perform better (the shorter the time, the better).
RollingFileAppender or ADONetAppender?
What are the other parameters I should consider when choosing an appender ?
I saw that my WebServer "holds on" to the rollingfile. Can I set something like
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
(reference: FileAppender section in log4net documentation) in my rolling file appender so that multiple applications/servers can write to the same file ?
our DB server is in a SAN drive, but the log files would be written locally to the hard drive (or may be a SAN drive in the near future)
If your ADONetAppender loses the connection, it will stop logging, but with the option
<reconnectonerror value="true" />
it will try to reconnect.
We have better luck using the rolling file appender. We've noticed that if there is a network glitch the ADO.NET appender stops logging. For example, a reboot after service pack installs on a weekly basis would kill your logger.
Keep in mind that also means keeping your logs on the same server if you are going with file appender - http://ferventcoder.com/archive/2009/07/16/log4net-note-always-keep-your-logs-on-the-same-server.aspx
I've never seen the minimal locking mechanism. It does look like you will pay some performance penalties if you do use it: http://logging.apache.org/log4net/release/sdk/log4net.Appender.FileAppender.MinimalLock.html
I generally use RollingFileAppender to a local disk - this is robust and fast (if you can't write to a local disk, you generally have a fatal problem).
Writing to ADO.NET can fail - for example if the logging database is taken offline for backup purposes while your application is running.
I would also avoid using MinimalLock because of the performance overhead - instead have a separate log file for each application. If you occasionally need to see consolidated log data, you can merge the relevant parts of the logs, or simply open the relevant parts side-by-side in a viewer.

Resources