Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why might log4net entries go "missing" in some listeners

Tags:

c#

log4net

This one really has me scratching my head....

I have been using log4net (currently version 1.2.10) in an application for some time. While adding a new option to the application, I noticed that even though the log4net Debug, Error, etc. methods were getting called items from that log source were not being seen by the console appender.

Having checked the obvious (like making sure there was no filtering involved), I noticed something else that was strange. If I have more than one appender (e.g. a log file appender and a UDP appender) then the appenders will sometimes see different subsets of the log messages. Which subset they see appears to be random, but typically when the problem occurs they will fail to see all messages from a given log source.

Why might this be happening, and what can I do about it since lost messages mean the log file cannot be trusted to show an accurate picture of remote failures?

[Additional information below added Jan 19th, 2010]

I finally took a good look at the ILog object getting passed back in response to the call

LogManager.GetLogger(typeof (MyTypeHere));

On some occasions, I am getting an ILog object with Debug, Info, Warning, Error etc set to false. On other occasions the ILog object has them correctly set to true. Since my code does nothing to manipulate those flags, on the occasions when my code is passed the "disabled" ILog object messages from my code (understandably) do not get propagated at all.

I still cannot explain the apparent discrepancy between the two appenders.

like image 531
Richard J Foster Avatar asked Jan 18 '10 16:01

Richard J Foster


People also ask

What are log4net Appenders?

For log4net to know where to store your log messages, you add one or more appenders to your configuration. An appender is a C# class that can transform a log message, including its properties, and persist it somewhere. Examples of appenders are the console, a file, a database, an API call, elmah.io, etc.

What are the log4net levels?

log4net offers the following log levels, in increasing order of priority: ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The ALL level logs everything and the OFF level logs nothing. You can assign these log levels to each logger in your configuration file.

How Logger works in c#?

You can now call the Log() method of the LogHelper class and pass the log target and the text message to log as parameters. If you ever need to log the text message to a different log target, you would simply pass the appropriate log target as a parameter to the Log() method of the LogHelper class.

Why log4net is used?

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.


2 Answers

We regularly use the logfile, console and smtp appenders together and we don't seem to have these issues. Under certain conditions, some appenders can lose messages because of their inherent nature. For example, the UDP appender, because of the transport mechanism, isn't guaranteed to transmit all messages. Same thing happens with the SMTP appender. If you are using a common log file but logging from several processes, sometimes the file is locked by another process (this usually throws an exception, but it might be getting caught somewhere in your code), so be sure to set the Minimal Lock property on it. Also, the appenders can be buffered, so if there is a process crash, log4net might not have a chance to flush out the buffered data.

In the end the most reliable appenders are those that log locally, such as the file and the event log appenders, but then you have to harvest all the logs. If you want to log centrally, you might want to consider logging to a database or a message queue.

like image 108
jvilalta Avatar answered Sep 29 '22 13:09

jvilalta


Do I understand correctly that some messages which are normally logged successfully suddennly stop appearing (being logged) at some point? If that is the case, then I would suggest turning on the internal logging of log4net. Alternativly debug the issue with log4net code (with your problem I'd suggest breaking somewhere around CallAppenders method in Logger class. It will tell you which appenders will actually be called for a logging event).

If some messages are consistently are not being logged then I would look at the log4net configuration. Check for any levels/thresholds being set and more importantly, if you are using loggers check that their names and make sure that the prefix of whatever you put into LogManager.GetLogger(...) call matches the name loggers in your config.

I double what jvilalta said. I have been using log4net for years now with many types of appenders and I have not seen a situation where a message would be missing from only some of appenders but not all.

like image 41
Konrad Avatar answered Sep 29 '22 11:09

Konrad