Ad

How To Conditionally Enable Actions In C# ASP.NET Website

- 1 answer

Using a configuration file I want to enable myself to turn on and off things like (third party) logging and using a cache in a C# website. The solution should not be restricted to logging and caching in particular but more general, so I can use it for other things as well.

I have a configuration xml file in which I can assert that logging and caching should be turned on or off (it could also be in the Web.Config, that's not the point right now) which will result in for example a bool logging and a bool caching that are true or false.

The question is about this part: What I can do is prepend every logging/caching related statement with if (logging) and if (caching). What is better way of programming this? Is there also a programming term for this kind of problem? Maybe attributes are also a way to go?

Ad

Answer

I'm curious what kind of logging/caching statements you have? If you have some class that is doing WriteLog or StoreCahce or whatever... why not just put the if(logging) in the WriteLog method. It seems like if you put all of your logging caching related methods into once class and that class knew whether logging/caching was on, then you could save your self a bunch of If statements at each instance.

Ad
source: stackoverflow.com
Ad