Dynamically change log levels confined to ThreadContext

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Dynamically change log levels confined to ThreadContext

Chris Landry

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!

Reply | Threaded
Open this post in threaded view
|

AW: Dynamically change log levels confined to ThreadContext

Dominik Psenner

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:[hidden email]]
Gesendet: Donnerstag, 12. Februar 2015 21:01
An: [hidden email]
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!