You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by ITemplate <it...@hotmail.com> on 2010/04/09 08:59:17 UTC

Possible to test log4net filters on certain logevent?

Hi,

Is this "old" forum the only for log4net? Anyways - suppose I have a certain
logevent, is it possible to investigate up front IF that particular logevent
would pass all filters? I can't seem to find a Filters collection that I can
use for this?

-- 
Werner
-- 
View this message in context: http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28188373.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Re: Possible to test log4net filters on certain logevent?

Posted by Loren Keagle <Lo...@aps-technology.com>.
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
>
>    


Re: Possible to test log4net filters on certain logevent?

Posted by ITemplate <it...@hotmail.com>.
Thanks Ron, very interesting. That interface also explains why log4net
accepts an object - I like this. I will look into your suggestions, thanks.

-- 
Werner



Ron Grabowski wrote:
> 
> Have you looked into writing an log4net.ObjectRenderer.IObjectRenderer to
> render the message as text or is that not that possibility?
> 
> Maybe this will work:
> 
> // untested
> public class WouldLogAppender : ForwardingAppender
> {
>  public bool WouldLog(LoggingEvent loggingEvent)
>  {
>  return FilterEvent(loggingEvent);
>  }
> }
> 
> You'd have to put your filters and thresholds on the WouldLogAppender
> instead of on the wrapped appender.
> 
> 
> ----- Original Message ----
> From: ITemplate <it...@hotmail.com>
> To: log4net-user@logging.apache.org
> Sent: Thu, April 15, 2010 3:36:20 AM
> Subject: Re: Possible to test log4net filters on certain logevent?
> 
> 
> 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...
> 
> -- 
> Werner
> 
> 
> 
> Loren Keagle wrote:
>> 
>> I don't think that this is possible without some dangerous coding.  
>> Looking at the source code, there are no methods on any appenders to 
>> test the level/filter chain in advance.  Given that your log4net config 
>> could contain dozens of appenders, each with their own set of filters, 
>> you could not make a single call to determine if an event would be 
>> filtered from all of them.  Perhaps if you provided a little bit of 
>> context, someone here might be able to offer an alternative approach?
>> 
>> If you really must have a programmatic way of doing this, you would have 
>> to gain access to all of your appenders, iterate through them, casting 
>> each to AppenderSkeleton (assuming the appender derives from this helper 
>> class!!!), test the log level of each, then iterate through all of the 
>> filters in the FilterHead linked list property.  AppenderSkeleton has a 
>> protected method called FilterEvent that does all of this, but you 
>> cannot access it.  It would probably have solved your problem if this 
>> method was part of the IAppender interface, but it's currently not 
>> accessible.
>> 
>> So I suppose it is possible, but you're going to end up duplicating all 
>> of the logic that is already in AppenderSkeleton, which means you will 
>> be executing code twice.  If you have multiple appenders, you will have 
>> to check each appender separately.
>> 
>> ~Loren Keagle
>> 
>> 
>> On 4/13/2010 6:18 AM, ITemplate wrote:
>>> Noone knows? Perhaps I could rephrase: I need a programmatic way to
>>> determine
>>> if a certain logentry would be filtered or not.
>>>
>>> Example: I have the following logentry:
>>> logger.Warn("This might be logged but I dont know. I want to test it in
>>> code.");
>>>
>>> Now I want to find out - at runtime - if that specific line in my code
>>> will
>>> produce a log. Some programmatic way to test one or all configured
>>> filters
>>> against the data in my log-line (Logger, Level, text etc).
>>>
>>> Hope my meaning here is more clearer?
>>>
>>> Thanks.
>>>
>>>
>>>
>>> ITemplate wrote:
>>>    
>>>> Hi,
>>>>
>>>> Is this "old" forum the only for log4net? Anyways - suppose I have a
>>>> certain logevent, is it possible to investigate up front IF that
>>>> particular logevent would pass all filters? I can't seem to find a
>>>> Filters
>>>> collection that I can use for this?
>>>>
>>>> -- 
>>>> Werner
>>>>
>>>>      
>>>    
>>>
>>>
>>>
>>> No virus found in this incoming message.
>>> Checked by AVG - www.avg.com
>>> Version: 9.0.801 / Virus Database: 271.1.1/2807 - Release Date: 04/12/10
>>> 11:32:00
>>>
>>>    
>> 
>> 
>> 
> 
> -- 
> View this message in context:
> http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28252058.html
> Sent from the Log4net - Users mailing list archive at Nabble.com.
> 
> 

-- 
View this message in context: http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28362157.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Re: Possible to test log4net filters on certain logevent?

Posted by Ron Grabowski <ro...@yahoo.com>.
Have you looked into writing an log4net.ObjectRenderer.IObjectRenderer to render the message as text or is that not that possibility?

Maybe this will work:

// untested
public class WouldLogAppender : ForwardingAppender
{
 public bool WouldLog(LoggingEvent loggingEvent)
 {
 return FilterEvent(loggingEvent);
 }
}

You'd have to put your filters and thresholds on the WouldLogAppender instead of on the wrapped appender.


----- Original Message ----
From: ITemplate <it...@hotmail.com>
To: log4net-user@logging.apache.org
Sent: Thu, April 15, 2010 3:36:20 AM
Subject: Re: Possible to test log4net filters on certain logevent?


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

-- 
Werner



Loren Keagle wrote:
> 
> I don't think that this is possible without some dangerous coding.  
> Looking at the source code, there are no methods on any appenders to 
> test the level/filter chain in advance.  Given that your log4net config 
> could contain dozens of appenders, each with their own set of filters, 
> you could not make a single call to determine if an event would be 
> filtered from all of them.  Perhaps if you provided a little bit of 
> context, someone here might be able to offer an alternative approach?
> 
> If you really must have a programmatic way of doing this, you would have 
> to gain access to all of your appenders, iterate through them, casting 
> each to AppenderSkeleton (assuming the appender derives from this helper 
> class!!!), test the log level of each, then iterate through all of the 
> filters in the FilterHead linked list property.  AppenderSkeleton has a 
> protected method called FilterEvent that does all of this, but you 
> cannot access it.  It would probably have solved your problem if this 
> method was part of the IAppender interface, but it's currently not 
> accessible.
> 
> So I suppose it is possible, but you're going to end up duplicating all 
> of the logic that is already in AppenderSkeleton, which means you will 
> be executing code twice.  If you have multiple appenders, you will have 
> to check each appender separately.
> 
> ~Loren Keagle
> 
> 
> On 4/13/2010 6:18 AM, ITemplate wrote:
>> Noone knows? Perhaps I could rephrase: I need a programmatic way to
>> determine
>> if a certain logentry would be filtered or not.
>>
>> Example: I have the following logentry:
>> logger.Warn("This might be logged but I dont know. I want to test it in
>> code.");
>>
>> Now I want to find out - at runtime - if that specific line in my code
>> will
>> produce a log. Some programmatic way to test one or all configured
>> filters
>> against the data in my log-line (Logger, Level, text etc).
>>
>> Hope my meaning here is more clearer?
>>
>> Thanks.
>>
>>
>>
>> ITemplate wrote:
>>    
>>> Hi,
>>>
>>> Is this "old" forum the only for log4net? Anyways - suppose I have a
>>> certain logevent, is it possible to investigate up front IF that
>>> particular logevent would pass all filters? I can't seem to find a
>>> Filters
>>> collection that I can use for this?
>>>
>>> -- 
>>> Werner
>>>
>>>      
>>    
>>
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - www.avg.com
>> Version: 9.0.801 / Virus Database: 271.1.1/2807 - Release Date: 04/12/10
>> 11:32:00
>>
>>    
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28252058.html
Sent from the Log4net - Users mailing list archive at Nabble.com.

RE: Possible to test log4net filters on certain logevent?

Posted by ITemplate <it...@hotmail.com>.
Thanks. I will take yours and Loren's thougts with me to our next developer
meeting. Although I'm new to log4net, I have to say that I'm willing to
fight to get this into our new applications. It is so easy to use compared
to our current (entlib) log system.

-- 
Werner


Walden H. Leverich wrote:
> 
> Werner, Loren's check of IsDebugEnabled is the accepted way to accomplish
> what you want. Beyond that though, if you're usage is like ours then most
> of the time (99.999999999999999999% :-) you don't want the logging of
> those large objects at all, but every once in a while you do. Well, we
> accepted that we'd have to restart the application to get it, and we added
> a static _readonly_ variable that we check in addition to IsDebugEnabled.
> Since it's static readonly, the JIT knows it can't be updated once it's
> set and will actually omit the logging from the generated machine code --
> it's one performance step beyond IsDebugEnabled.
> 
> -Walden
> 
> 
> -- 
> Walden H Leverich III
> Tech Software & 
> BEC - IRBManager
> (516) 627-3800 x3051
> WaldenL@TechSoftInc.com
> http://www.TechSoftInc.com
> http://www.IRBManager.com
> 
> Quiquid latine dictum sit altum viditur.
> (Whatever is said in Latin seems profound.)
> 
> 
> -----Original Message-----
> From: ITemplate [mailto:itemplate@hotmail.com] 
> Sent: Thursday, April 15, 2010 3:36 AM
> To: log4net-user@logging.apache.org
> Subject: Re: Possible to test log4net filters on certain logevent?
> 
> 
> 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...
> 
> -- 
> Werner
> 
> 
> 
> Loren Keagle wrote:
>> 
>> I don't think that this is possible without some dangerous coding.  
>> Looking at the source code, there are no methods on any appenders to 
>> test the level/filter chain in advance.  Given that your log4net config 
>> could contain dozens of appenders, each with their own set of filters, 
>> you could not make a single call to determine if an event would be 
>> filtered from all of them.  Perhaps if you provided a little bit of 
>> context, someone here might be able to offer an alternative approach?
>> 
>> If you really must have a programmatic way of doing this, you would have 
>> to gain access to all of your appenders, iterate through them, casting 
>> each to AppenderSkeleton (assuming the appender derives from this helper 
>> class!!!), test the log level of each, then iterate through all of the 
>> filters in the FilterHead linked list property.  AppenderSkeleton has a 
>> protected method called FilterEvent that does all of this, but you 
>> cannot access it.  It would probably have solved your problem if this 
>> method was part of the IAppender interface, but it's currently not 
>> accessible.
>> 
>> So I suppose it is possible, but you're going to end up duplicating all 
>> of the logic that is already in AppenderSkeleton, which means you will 
>> be executing code twice.  If you have multiple appenders, you will have 
>> to check each appender separately.
>> 
>> ~Loren Keagle
>> 
>> 
>> On 4/13/2010 6:18 AM, ITemplate wrote:
>>> Noone knows? Perhaps I could rephrase: I need a programmatic way to
>>> determine
>>> if a certain logentry would be filtered or not.
>>>
>>> Example: I have the following logentry:
>>> logger.Warn("This might be logged but I dont know. I want to test it in
>>> code.");
>>>
>>> Now I want to find out - at runtime - if that specific line in my code
>>> will
>>> produce a log. Some programmatic way to test one or all configured
>>> filters
>>> against the data in my log-line (Logger, Level, text etc).
>>>
>>> Hope my meaning here is more clearer?
>>>
>>> Thanks.
>>>
>>>
>>>
>>> ITemplate wrote:
>>>    
>>>> Hi,
>>>>
>>>> Is this "old" forum the only for log4net? Anyways - suppose I have a
>>>> certain logevent, is it possible to investigate up front IF that
>>>> particular logevent would pass all filters? I can't seem to find a
>>>> Filters
>>>> collection that I can use for this?
>>>>
>>>> -- 
>>>> Werner
>>>>
>>>>      
>>>    
>>>
>>>
>>>
>>> No virus found in this incoming message.
>>> Checked by AVG - www.avg.com
>>> Version: 9.0.801 / Virus Database: 271.1.1/2807 - Release Date: 04/12/10
>>> 11:32:00
>>>
>>>    
>> 
>> 
>> 
> 
> -- 
> View this message in context:
> http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28252058.html
> Sent from the Log4net - Users mailing list archive at Nabble.com.
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28265740.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


RE: Possible to test log4net filters on certain logevent?

Posted by "Walden H. Leverich" <Wa...@TechSoftInc.com>.
Werner, Loren's check of IsDebugEnabled is the accepted way to accomplish what you want. Beyond that though, if you're usage is like ours then most of the time (99.999999999999999999% :-) you don't want the logging of those large objects at all, but every once in a while you do. Well, we accepted that we'd have to restart the application to get it, and we added a static _readonly_ variable that we check in addition to IsDebugEnabled. Since it's static readonly, the JIT knows it can't be updated once it's set and will actually omit the logging from the generated machine code -- it's one performance step beyond IsDebugEnabled.

-Walden


-- 
Walden H Leverich III
Tech Software & 
BEC - IRBManager
(516) 627-3800 x3051
WaldenL@TechSoftInc.com
http://www.TechSoftInc.com
http://www.IRBManager.com

Quiquid latine dictum sit altum viditur.
(Whatever is said in Latin seems profound.)


-----Original Message-----
From: ITemplate [mailto:itemplate@hotmail.com] 
Sent: Thursday, April 15, 2010 3:36 AM
To: log4net-user@logging.apache.org
Subject: Re: Possible to test log4net filters on certain logevent?


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

-- 
Werner



Loren Keagle wrote:
> 
> I don't think that this is possible without some dangerous coding.  
> Looking at the source code, there are no methods on any appenders to 
> test the level/filter chain in advance.  Given that your log4net config 
> could contain dozens of appenders, each with their own set of filters, 
> you could not make a single call to determine if an event would be 
> filtered from all of them.  Perhaps if you provided a little bit of 
> context, someone here might be able to offer an alternative approach?
> 
> If you really must have a programmatic way of doing this, you would have 
> to gain access to all of your appenders, iterate through them, casting 
> each to AppenderSkeleton (assuming the appender derives from this helper 
> class!!!), test the log level of each, then iterate through all of the 
> filters in the FilterHead linked list property.  AppenderSkeleton has a 
> protected method called FilterEvent that does all of this, but you 
> cannot access it.  It would probably have solved your problem if this 
> method was part of the IAppender interface, but it's currently not 
> accessible.
> 
> So I suppose it is possible, but you're going to end up duplicating all 
> of the logic that is already in AppenderSkeleton, which means you will 
> be executing code twice.  If you have multiple appenders, you will have 
> to check each appender separately.
> 
> ~Loren Keagle
> 
> 
> On 4/13/2010 6:18 AM, ITemplate wrote:
>> Noone knows? Perhaps I could rephrase: I need a programmatic way to
>> determine
>> if a certain logentry would be filtered or not.
>>
>> Example: I have the following logentry:
>> logger.Warn("This might be logged but I dont know. I want to test it in
>> code.");
>>
>> Now I want to find out - at runtime - if that specific line in my code
>> will
>> produce a log. Some programmatic way to test one or all configured
>> filters
>> against the data in my log-line (Logger, Level, text etc).
>>
>> Hope my meaning here is more clearer?
>>
>> Thanks.
>>
>>
>>
>> ITemplate wrote:
>>    
>>> Hi,
>>>
>>> Is this "old" forum the only for log4net? Anyways - suppose I have a
>>> certain logevent, is it possible to investigate up front IF that
>>> particular logevent would pass all filters? I can't seem to find a
>>> Filters
>>> collection that I can use for this?
>>>
>>> -- 
>>> Werner
>>>
>>>      
>>    
>>
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - www.avg.com
>> Version: 9.0.801 / Virus Database: 271.1.1/2807 - Release Date: 04/12/10
>> 11:32:00
>>
>>    
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28252058.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Re: Possible to test log4net filters on certain logevent?

Posted by ITemplate <it...@hotmail.com>.
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...

-- 
Werner



Loren Keagle wrote:
> 
> I don't think that this is possible without some dangerous coding.  
> Looking at the source code, there are no methods on any appenders to 
> test the level/filter chain in advance.  Given that your log4net config 
> could contain dozens of appenders, each with their own set of filters, 
> you could not make a single call to determine if an event would be 
> filtered from all of them.  Perhaps if you provided a little bit of 
> context, someone here might be able to offer an alternative approach?
> 
> If you really must have a programmatic way of doing this, you would have 
> to gain access to all of your appenders, iterate through them, casting 
> each to AppenderSkeleton (assuming the appender derives from this helper 
> class!!!), test the log level of each, then iterate through all of the 
> filters in the FilterHead linked list property.  AppenderSkeleton has a 
> protected method called FilterEvent that does all of this, but you 
> cannot access it.  It would probably have solved your problem if this 
> method was part of the IAppender interface, but it's currently not 
> accessible.
> 
> So I suppose it is possible, but you're going to end up duplicating all 
> of the logic that is already in AppenderSkeleton, which means you will 
> be executing code twice.  If you have multiple appenders, you will have 
> to check each appender separately.
> 
> ~Loren Keagle
> 
> 
> On 4/13/2010 6:18 AM, ITemplate wrote:
>> Noone knows? Perhaps I could rephrase: I need a programmatic way to
>> determine
>> if a certain logentry would be filtered or not.
>>
>> Example: I have the following logentry:
>> logger.Warn("This might be logged but I dont know. I want to test it in
>> code.");
>>
>> Now I want to find out - at runtime - if that specific line in my code
>> will
>> produce a log. Some programmatic way to test one or all configured
>> filters
>> against the data in my log-line (Logger, Level, text etc).
>>
>> Hope my meaning here is more clearer?
>>
>> Thanks.
>>
>>
>>
>> ITemplate wrote:
>>    
>>> Hi,
>>>
>>> Is this "old" forum the only for log4net? Anyways - suppose I have a
>>> certain logevent, is it possible to investigate up front IF that
>>> particular logevent would pass all filters? I can't seem to find a
>>> Filters
>>> collection that I can use for this?
>>>
>>> -- 
>>> Werner
>>>
>>>      
>>    
>>
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - www.avg.com
>> Version: 9.0.801 / Virus Database: 271.1.1/2807 - Release Date: 04/12/10
>> 11:32:00
>>
>>    
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28252058.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Re: Possible to test log4net filters on certain logevent?

Posted by Loren Keagle <Lo...@aps-technology.com>.
I don't think that this is possible without some dangerous coding.  
Looking at the source code, there are no methods on any appenders to 
test the level/filter chain in advance.  Given that your log4net config 
could contain dozens of appenders, each with their own set of filters, 
you could not make a single call to determine if an event would be 
filtered from all of them.  Perhaps if you provided a little bit of 
context, someone here might be able to offer an alternative approach?

If you really must have a programmatic way of doing this, you would have 
to gain access to all of your appenders, iterate through them, casting 
each to AppenderSkeleton (assuming the appender derives from this helper 
class!!!), test the log level of each, then iterate through all of the 
filters in the FilterHead linked list property.  AppenderSkeleton has a 
protected method called FilterEvent that does all of this, but you 
cannot access it.  It would probably have solved your problem if this 
method was part of the IAppender interface, but it's currently not 
accessible.

So I suppose it is possible, but you're going to end up duplicating all 
of the logic that is already in AppenderSkeleton, which means you will 
be executing code twice.  If you have multiple appenders, you will have 
to check each appender separately.

~Loren Keagle


On 4/13/2010 6:18 AM, ITemplate wrote:
> Noone knows? Perhaps I could rephrase: I need a programmatic way to determine
> if a certain logentry would be filtered or not.
>
> Example: I have the following logentry:
> logger.Warn("This might be logged but I dont know. I want to test it in
> code.");
>
> Now I want to find out - at runtime - if that specific line in my code will
> produce a log. Some programmatic way to test one or all configured filters
> against the data in my log-line (Logger, Level, text etc).
>
> Hope my meaning here is more clearer?
>
> Thanks.
>
>
>
> ITemplate wrote:
>    
>> Hi,
>>
>> Is this "old" forum the only for log4net? Anyways - suppose I have a
>> certain logevent, is it possible to investigate up front IF that
>> particular logevent would pass all filters? I can't seem to find a Filters
>> collection that I can use for this?
>>
>> -- 
>> Werner
>>
>>      
>    
>
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 9.0.801 / Virus Database: 271.1.1/2807 - Release Date: 04/12/10 11:32:00
>
>    


Re: Possible to test log4net filters on certain logevent?

Posted by ITemplate <it...@hotmail.com>.
Noone knows? Perhaps I could rephrase: I need a programmatic way to determine
if a certain logentry would be filtered or not.

Example: I have the following logentry: 
logger.Warn("This might be logged but I dont know. I want to test it in
code.");

Now I want to find out - at runtime - if that specific line in my code will
produce a log. Some programmatic way to test one or all configured filters
against the data in my log-line (Logger, Level, text etc). 

Hope my meaning here is more clearer?

Thanks.



ITemplate wrote:
> 
> Hi,
> 
> Is this "old" forum the only for log4net? Anyways - suppose I have a
> certain logevent, is it possible to investigate up front IF that
> particular logevent would pass all filters? I can't seem to find a Filters
> collection that I can use for this?
> 
> -- 
> Werner
> 

-- 
View this message in context: http://old.nabble.com/Possible-to-test-log4net-filters-on-certain-logevent--tp28188373p28229862.html
Sent from the Log4net - Users mailing list archive at Nabble.com.