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 Christopher Landry <cl...@igloosoftware.com> on 2015/02/12 21:00:49 UTC

Dynamically change log levels confined to ThreadContext

I have a web app, and I'm trying to allow users to set the logging level via a query parameter. Got this working pretty easily.

The issue is that it doesn't just change the logging level for that request, it's changed for all requests that come in during the life cycle of that request - since I'm setting it back to the default level when the request is resolved.

I have tried adding the following in my appender, but it's not working as I hoped.

<evaluator type="log4net.spi.LevelEvaluator">
     <threshold>
           <layout type="log4net.Layout.RawPropertyLayout">
                <key value="LogLevel" />
     </threshold>
</evaluator>

And set a ThreadContext property called LogLevel to the appropriate level, but that doesn't seem to work either.

Any thoughts? Have I missed something obvious?

Thanks!

AW: Dynamically change log levels confined to ThreadContext

Posted by Dominik Psenner <dp...@gmail.com>.
Hi Christopher,

 

I believe that you would have to create your own level evaluator
implementation. Basically you implement the interface log4net.Filter.IFilter
or, to make the interaction with the filter chain less painful, you extend
log4net.Filter.FilterSkeleton. There you can override the method
Decice(LoggingEvent) and fit it to your needs. Last but not least you will
then configure log4net to set up your filter in the configuration and you
can do that just like one would reference a custom appender
(http://logging.apache.org/log4net/release/faq.html#custom-appender).

 

Best regards,

Dominik

 

Von: Christopher Landry [mailto:clandry@igloosoftware.com] 
Gesendet: Donnerstag, 12. Februar 2015 21:01
An: log4net-user@logging.apache.org
Betreff: Dynamically change log levels confined to ThreadContext

 

I have a web app, and I'm trying to allow users to set the logging level via
a query parameter. Got this working pretty easily.

 

The issue is that it doesn't just change the logging level for that request,
it's changed for all requests that come in during the life cycle of that
request - since I'm setting it back to the default level when the request is
resolved.

 

I have tried adding the following in my appender, but it's not working as I
hoped.

 

<evaluator type="log4net.spi.LevelEvaluator">

     <threshold>

           <layout type="log4net.Layout.RawPropertyLayout">

                <key value="LogLevel" />

     </threshold>

</evaluator>

 

And set a ThreadContext property called LogLevel to the appropriate level,
but that doesn't seem to work either.

 

Any thoughts? Have I missed something obvious?

 

Thanks!