I think you can get halfway to your goal just by using the suggestions in the FAQ. If you only care to check the log level of your logger instance, and not the filter chains in the individual appenders, you can use the helper methods on the ILog interface.

if (logger.IsDebugEnabled)
{
    string s = customObject.Serialize();
    logger.Debug("Custom serialized object: " + s);
}

Change the signature appropriately for Info, Error, Warn, or Fatal logging.

If you do need to check if the logging would get past filters on the individual appenders, you'll have to download the source code and simply add the log4net project to your solution so you can edit the source. We do this in order to support custom logging levels lower than the Debug level. You could easily add a helper method to the ILog interface to check if an event would make it past all of the filters on all of the appenders. In the LogImpl class, create an implementation that iterates through all of the IAppenders and calls the FilterEvent method.

However, since your custom object has not yet been serialized, filters that actually analyze the content of the log message would obviously not trigger! Hopefully the first solution will be all you need.

~Loren Keagle

On 4/15/2010 12:36 AM, ITemplate wrote:
Hi Loren,

Thanks for your reply! Well there are a couple of reasons but as an example,
we need to log special webservices where the request and responses are VERY
large objects that log4net (or any other) can't serialize correct. We have
our own serializer for that job. But at the same time, these web services
must perform well and object serialization here is a rather expensive and
time consuming task. So I would like the ability to test if the log system
would actually log the data - and if not, I would not even begin to
serialize the classes to text.

But on the other hand I will not begin to dig too deep into log4net to
accomplish this - it would only make sense if there were some easy way like:

var mustSerialize = false
foreach(var appender in log4net.Appenders)
{
   mustSerialize = appender.WouldLog(logEvent);
   if (mustSerialize) break;
}

But digging deeper would drain the goal of saving the time to serialize I
guess...



No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.801 / Virus Database: 271.1.1/2811 - Release Date: 04/14/10 
11:31:00


Reply via email to