tags:

views:

201

answers:

6

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?

+6  A: 

Why not just use the web.config and the System.Configuration functionality that already exists?

Your web app is going to parse web.config on every page load anyway, so the overhead involved in having yet another XML config file seems overkill when you can just define your own section on the existing configuration.

foxxtrot
Yes, that is possible, but I was more curious of how to handle these conditions in the code.
Michiel Borkent
Changed the question accordingly, thanks so far.
Michiel Borkent
As an FYI - the Web.Config is only parsed once on AppStart and again OnFileChange
stephbu
Thanks for the clarification Stephbu. While correct, I don't think it changes the point I was making.
foxxtrot
A: 

Consult http://msdn.microsoft.com/en-us/library/ms178606.aspx for specifics regarding configuring cache.

ddc0660
Sorry, I think I didn't formulate the question clear enough. The solution I'm after is not about caching in particular, but about how to program these kind of cases in general. I changed the question.
Michiel Borkent
A: 

I agree with foxxtrot, you want to use the web.config and add in a appsetting or two to hold the values.

Then for the implementation on checking, yes, simply use an if to see if you need to do the action. I highly recommend centralizing your logging classes to prevent duplication of code.

Mitchel Sellers
A: 

You could use a dependency injection container and have it load different logging and caching objects based on configuration. If you wanted to enable logging, you would specify an active Logging object/provider in config; if you wanted to then disable it, you could have the DI inject a "dummy" logging provider that did not log anything but returned right away.

I would lean toward a simpler design such as the one proposed by @foxxtrot, but runtime swapping out of utility components is one of the things that DI can do for you that is kind of nice.

Guy Starbuck
+1  A: 

You could check out the Microsoft Enterprise Library. It features stuff like logging and caching. The logging is made easy by the fact you always include the logging code but the actual logging beneath it is controlled by the settings.

http://msdn.microsoft.com/en-us/library/cc467894.aspx

You can find other cool stuff in the patterns and practices group.

Per Hornshøj-Schierbeck
stephbu
+2  A: 

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.

Jeff Martin
Probably this is the easiest way to solve it, to write a wrapper class or maybe even just a delegate that does the checking.
Michiel Borkent
I wondered if some people might also come up with things like attributes.
Michiel Borkent